Linux script change file name,folder name and ingridents - linux

I have a long folder/file structure with bunch of code files in it. some of my files has "x5g6" pattern on their name, on the folder name and also the text inside the files.
e.g
/Mycodes
/pp_x5g6
- vbg_x5g6.cmd
- x5g6_pp
- x5g6_pp.ml
so on so forth
also if you open vbg_x5g6.cmd file you can see there is a code in it and it also has this pattern (e.g function bb_x5g6 = x+ y);
My question is which commands I can use to recursively change x5g6 into x5g7
on folder, file names and also inside the files?
So far I could only found;
find . -type f -exec sed -i 's/x5g6/x5g7/g' {} +
but this only changes whatever inside the files not the folder and file names.

It looks like you have a solution already for editing the file contents.
For the file/directory names, I believe the generally accepted answers are to use either a program called mmv, (which I, myself, prefer), or one called rename
For the record, this question is a duplicate of https://unix.stackexchange.com/questions/98070/rename-files-in-directory.
The original there contains an answer also recommending zmv (if you're using zsh instead of bash).
edit: grammar

Related

Linux rename multiple files

I have some files named in a specifc pattern, for example, ab_2000_1.jpg. In this name 2000 is representing years and 1 representing month(1 means january). I have a 20 years of monthly files like this.
Now I want to rename every one of them into the following format ab_2000_1_12.jpg, ab_2000_2_12.jpg, etc
I know how to rename files using rename and sed command. But I want to know how can I loop this command for all files.
Any help is highly appreciated.
You can use a for loop to loop over all file names matching a pattern as for file in pattern; do some_commands; done.
You don't need sed to modify the file name in this case. A variable substitution like ${variable%pattern} will remove the shortest string matching pattern from the end of the variable value.
The following example code will remove .jpg from the end of the file name and append _12.jpg to the result.
for file in ab_*_*.jpg
do
mv "$file" "${file%.jpg}_12.jpg"
done

Renaming All Files in a Directory

I split a large text file into 60 chunks, which are are named xaa, xab, xac,...xcg. I want to rename these files so that they all end with .txt
How can I do this from the linux command line?
Looked in the split command for the ability to customize the filenames. Looked on Stack Overflow for other solutions but the ones I've come across are all too specific to the OP's situation.
Assuming that your shell is the default Bash:
for f in x??; do mv "$f" "$f.txt"; done
If you want to be more specific, you could say x[abc][a-z] instead of x??.
This is good enough for a one-liner. In a script you would want to check that "$f" exists before trying to rename it.

Search and replace files (Linux)

I'm quite new to Linux. I'm using Linux Mint and I've just found a situation where I have a file which exists multiple times inside the tree/folders of a folder. I want to replace all occurrences of this file with a new version of it.
So instead of looking for that file once and again and replacing it with the new one, I wonder if there is any kind of search & replace command for files.
I've already searched for a similar question in stackoverflow, but I was only able to find commands to search & replace TEXT in files, not the file itself.
Can anyone please point me to the right direction?
Thank you.
you can always do it in parts, like:
Get a list of items matching your search.
Replace every match (using mv for example) with your file.
something like:
foreach dir ( `ls | egrep '^(i686\|amd64)\.'` )
mv yourfile $dir
end

How to remove part of file names between periods?

I would like to rename many files in this format
abc.123.fits
abcd.1234.fits
efg.12.fits
to this format
abc.fits
abcd.fits
efg.fits
I tried the rename function, but since the part I'm trying to replace is not the same in all files, it did not work. I am using Linux.
for f in *; do mv "$f" "${f%%.*}.${f##*.}"; done`
${f%%.*} removes everything after the first period, including the period. ${f##*.} removes everything before the last period, including the period (i.e. it gets the file extension). Concatenating these two, with a period between them, gives you the desired result.
You can change the * to a more restrictive pattern such as *.fits if you don't want to rename all files in the current directory. The quotes around the parameters to mv are necessary if any filenames contain whitespace.
Many other variable substitution expressions are available in bash; see a reference such as TLDP's Bash Parameter Substitution for more information.

how to change a single letter in filenames all over the file system?

i have hundreds of files with special characters ('æ', 'ø' and 'å') in their filenames.
i cannot copy these to my external mntfs disk without renaming.
the files are in dozens of different folders. there are thousands of other files without these letters in there as well.
i'd like to replace the special characters with their placeholders ('ae', 'oe' and 'aa'), while keeping the rest of the filename intact.
i'm on ubuntu. i'm thinking of using grep, sed and tr, but i don't know exactly how.
You can use rename command from util-linux package.
For example,
find / -type f -exec rename 'â' 'a' {} \;
convmv is used to convert filenames between encodings. I'm sure it can solve your problem, even if it might not be exactly what you asked for.

Resources