Looking for help with the following code. I have a folder titled data, with 6 subfolders (folder1, folder2, etc). Each folder has a text file I want to rename to "notes" keeping the .txt extension.
So far I have the following code:
for file in data/*/*.txt; do
mv $file "notes"
done
Instead of renaming, I am getting a "notes.exec" file on my desktop. Does anyone have any suggestions?
When I echo my first line I know I am getting the proper files:
for file in data/*/*.txt; do
echo $file
done
data/folder1/day4notes.txt
data/folder2/lecturenotes.txt
etc.
CodePudding user response:
You can use find command with -execdir that will execute command of your choice in a directory where file matching pattern is:
find data -type f -name '*.txt' -execdir mv \{\} notes.txt \;
datais path to directory wherefindshould look for matching files recursively-type flook only for files, not directories-name '*.txt'match anything that ends with.txt-execdir mv \{\} notes.txtrun commandmv {} notes.txtin directory where file was found; where{}is original filename found byfind.
NOTE: When experimenting with things like these always make backup beforehand. If you have multiple .txt files in any of directories they all will get renamed to notes.txt so you might lose data in that case.
CodePudding user response:
I don't have the reputation to comment, but this is a duplicate question. You can find a more exact answer to your question here. This has the exact type of loop that you are using but also indicates how to format the "mv" line so that it renames how you want.
Rename all files in directory from $filename_h to $filename_half?
