Change all file name and extension in directory in linux or windows - linux

I have thousands of file with file extensions like this
3_bedroom_villas_in_chennai.html__201308050010_
3_bedroom_villas_in_chennai.html__201308080012_
3_bedroom_villas_in_chennai.html__201308100012_
3_bedroom_villas_in_chennai.html__201308110034_ and so on.....
inside a directory. I wanna change all these into the following
3_bedroom_villas_in_chennai__201308050010_.html
3_bedroom_villas_in_chennai__201308080012_.html
3_bedroom_villas_in_chennai__201308100012_.html
3_bedroom_villas_in_chennai__201308110034_.html
When I tried doing it in windows using the following command
ren *.* *.html
I got the following
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found. and so on...
Because I know that it will try to change everything into a single file name like
3_bedroom_villas_in_chennai.html and so on...
any ways to do this either on windows or linux ??

In Linux:
ls | xargs -I % mv % %.html
ls command output is piped to xargs, and xargs replaces all % (after mv) with the input from ls
Also, if you want recursively go through all sub-directories, you might want to use:
find . -type f | xargs -I % mv % %.html
And in Windows:
for /r %x in (*) do ren "%x" *.txt

using renamer:
$ renamer --regex --find '(.*).html(.*)' --replace '$1$2.html' *
Works on Windows, Mac and Linux.

Related

Adding a line to multiple files in directory

I have a line to be added to 3rd line of all files in directory. What's the commandline to do this operation
Lets say I want to add "color #c2451 " to 3rd line of files in Class directory
Try and use the find cmd piped into xargs and the sed cmd.
You'd have to cd into the directory first with the files.
find . -type f -name '*' | xargs sed -i "3i color #c2451"
Add text to file at certain line in Linux
Change multiple files

How to find and replace an IP address in many archives in linux

Example:
find /tmp/example -type f -print0 | xargs -0 sed -i 's/10.20.1.110/10.10.1.40/g'
I need replace 10.20.1.110 to 10.10.1.40 in all archives inside /tmp/example.
But this command does not replace inside archives.
.xml, *.txt , *.py ..jy . This archives types.
These are not archives, but ordinary text file extensions; thus, if the sed command doesn't work for you, there must be another reason. It may be that the command is executed with insufficient priviledges - sed -i exits as soon as it cannot rename its temporary output file to the input file (as it's the case if the containing directory has the sticky bit t set and you don't own the file or the directory). Pay heed to error messages.

Find a zip file, print path and zip contents

I have a series of numbered sub-directories that may or may not contain zip files, and within those zip files are some single-line .txt files I need. Is it possible to use a combination of find and unzip -p to list the file path and the single line contents on the same output line? I'd like to save the results to a .txt and import it into excel to work with.
From the main directory I can successfully find and output the single line:
find . -name 'file.zip' -exec unzip -p {} file.txt \;
How can I prefix the find output (i.e. the file path) to the output of this unzip command? Ideally, I'd like each line of the text file to resemble:
./path/to/file1.zip "Single line of file1.txt file"
./path/to/file2.zip "Single line of file2.txt file"
and so on. Can anyone provide some suggestions? I'm not very experienced with linux command line beyond simple commands.
Thank you.
Put all the code you want to execute into a shell script, then use the exec feature to call the shell script, i.e.
cat finder.bash
#!/bin/bash
printf "$# : " # prints just the /path/to/file/file.zip
unzip -p "$#" file.txt
For now, get that to work, you can make it generic to pass others besides file.txt later.
Make the script executable
chmod 755 finder.bash
Call it from find. i.e.
find . -name 'file.zip' -exec /path/to/finder.bash {} \;
(I don't have an easy way to test this, so reply in comments with error msgs).

I would like to add a line in a file on a linux shell

I am new to the Linux Command Line and truth be told, I am a little intimidated be all the commands. I would like to add a line between find. What commands are there for? There is a lot out there about files but nothing about the content.
cd /applbeh/cr/acr/xfb/send/
find Mt940 -type f -iname '*MT940*' | xargs -r rm -v
find Mt940 -type f -iname '*OVK*' | xargs -r rm -v
Thanks
A oneliner to add a line to a file is:
echo 'line' >> file
With line (without the quotes), the content of the line you wish to add and file the name of the file you wish to write to.
>> is an IO redirection that means append, it means you add the given output (echo 'line') thus line to the file.

Removing Colons From Multiple FIles on Linux

I am trying to take some directories that and transfer them from Linux to Windows. The problem is that the files on Linux have colons in them. And I need to copy these directories (I cannot alter them directly since they are needed as they are the server) over to files with a name that Windows can use. For example, the name of a directory on the server might be:
IAPLTR2b-ERVK-LTR_chr9:113137544-113137860_-
while I need it to be:
IAPLTR2b-ERVK-LTR_chr9-113137544-113137860_-
I have about sixty of these directories and I have collected the names of the files with their absolute paths in a file I call directories.txt. I need to walk through this file changing the colons to hyphens. Thus far, my attempt is this:
#!/bin/bash
$DIRECTORIES=`cat directories.txt`
for $i in $DIRECTORIES;
do
cp -r "$DIRECTORIES" "`echo $DIRECTORIES | sed 's/:/-/'`"
done
However I get the error:
./my_shellscript.sh: line 10: =/bigpartition1/JKim_Test/test_bs_1/129c-test-biq/IAPLTR1_Mm-ERVK-LTR_chr10:104272652-104273004_+.fasta: No such file or directory ./my_shellscript.sh: line 14: `$i': not a valid identifier
Can anyone here help me identify what I am doing wrong and maybe what I need to do?
Thanks in advance.
This monstrosity will rename the directories in situ:
find tmp -depth -type d -exec sh -c '[ -d "{}" ] && echo mv "{}" "$(echo "{}" | tr : _)"' \;
I use -depth so it descends down into the deepest subdirectories first.
The [ -d "{}" ] is necessary because as soon as the subdirectory is renamed, its parent directory (as found by find) may no longer exist (having been renamed).
Change "echo mv" to "mv" if you're satisfied it will do what you want.

Resources