Rename files in different folders removing numbers from file names - unix

I'm trying to remove numbers from file names stored in different folders.
Specifically, I have 100 folders named: my_folder1, my_folder2, my_folder3,..., my_folder100.
In each folder there are files named: my_folder1.txt for my_folder1, my_folder2.txt for my_folder2, my_folder3.txt for my_folder3, ...my_folder100.txt for my_folder100. I need the following output:
my_folder.txt for my_folder1, my_folder.txt for my_folder2, my_folder.txt for my_folder3, ..., my_folder.txt for my_folder100. In other words I need to remove the numbers from file names in each folder. I used the following code:
for file in `find . -name 'my_folder*.txt'`; do
mv $file ${file/+([0-9]).txt/.txt}
done
but the numbers are still there.
Can anyone help me please?
Best

Are you looking for:
for file in */*.txt; do mv $file $(dirname $file)/my_folder.txt; done

Related

how to list all files in a directory which contain only one dot

I am trying to get all files in a directory which contain only one extension dot such as abcd.py and not abcd.efg.py
i've tried ls ~/scripts/[^.].py with no success
eventually, I used: find ~/scripts/ -regex "[^.]*.py"

How to rename file from the specific pattern within the file

I would like to change the file name according to the specific pattern within the file. Let's say I have the unique pattern that starts with "XmacTmas". I would like to use this pattern to rename the file with the additional character like "_dbp1".
Now my file name is "xxo1" and I want "XmacTmas_dbp1".
How can I do this in for thousands of files with some script.
Thanks
find . -name 'XmacTmas*' -exec echo mv {} {}_dbp1 \;
find the files of interest and execute command after replacing {} with the found filename.
Escape the ;. Without the \, find would take it as part of the command to execute.
If only files in the actual directory are needed, add -maxdepth 0 before -name (or any other of find's numerous options)
If the result is as needed, remove the echo

Find all filename that contains a particular substring in them

I wanted to write a command that would help me fetch recursively in a folder all filenames that have a particular text in them . Suppose my folder contains lot of files two of them being largest_pallindrome_subsequence_1.cpp and largest_pallindrome_subsequence_2.cpp . Now I want to find files which have sub in it . So the search should return me these 2 cpp files as mentioned above.
The thing is that I also want to look for a file with particular extension say .txt or .cpp .
I tried using grep --include=\*{.cpp} -rnw . -e "sub" but this doesnot work for me.
You can do:
find ./ -name "*sub*"
or:
find ./ | grep "sub"

Rename many images with names from file

I have a file with a list of names. Let's call it nameFile. For example:
John Doe
John Benjamin
Benjamin Franklin
...
I also have a folder of pictures. The pictures are named like:
pic001.jpg
pic002.jpg
pic003.jpg
...
I want to rename each picture with the corresponding name from the nameFile. Thus, pic001.jpg will become 'John Doe.jpg', pic002.jpg will become 'John Benjamin.jpg', etc.
Is there an easy UNIX command to do this? I know mv can be used to rename, I'm just a bit unsure how to apply it to this situation.
Mostly people do it by writing a simple shell script.
These two links will help you to do it.
Bulk renaming of files in unix
Rename a group of files with one command
The mv is a Unix command that renames one or more files or directories. The original filename or directory name is no longer accessible. Write permission is required on all directories and files being modified.
mv command syntax
You need to use the mv command to rename a file as follows:
mv old-file-name new-file-name
mv file1 file2
mv source target
mv [options] source target

Merging file names into one text file

I have 8 files that need to be merged into one text file, with each of the file names being on a separate line.
The output should be as follows:
file.txt:
output1/transcripts.gtf
output2/transcripts.gtf
output3/transcripts.gtf
and so on...
I have read several other suggestions and I know it should be an easy fix. I have tried dir and awk but have only gotten results that has all files in one line. I am using unix.
How about this?
ls -1 output*/*.gtf > file.txt
or if the nesting of you sub directories is deeper and you want all files with names ending in ".gtf":
find . -type f -name "*.gtf" -print | cut -b 3- > file.txt

Resources