'Rename .txt files
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 "homeworknotes" keeping the .txt extension.
Used notes before for short:
So far I have the following code:
for file in data/*/*.txt; do
mv $file "notes"
done
Solution 1:[1]
find
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.
bash
EDIT1: To do this without find you need to handle recursive directory traversal (unless you have fixed directory layout). In bash you can set following shell options with shopt -s command:
extglob- extended globbing support (allows to write extended globs like**; see "Pathname Expansion" inman bash)globstar- allows**in pathname expansion;**/will match any directories and their subdirectories (see "Pathname Expansion" inman bash).nullglob- allows patterns that match no files (in case there's a directory without any .txt file)
Following script will traverse directories under data/ and rename .txt files to notes.txt:
#!/bin/bash
shopt -s extglob globstar nullglob
for f in data/**/*.txt ; do
mv $f $(dirname $f)/notes.txt
done
mv $f $(dirname $f)/notes.txtmoves (renames) file;$fcontains matched path so e.g.data/folder1/day4notes.txtand$(dirname $f)gets directory where that file is - in this casedata/folder1so we just append/notes.txtto that.
EDIT2: If you are absolutely positive that you want to do this only in first level of subdirectories under data/ you can omit extglob and globstar (and if you know there's at least one .txt in each directory then also nullglob) and go ahead with pattern you posted; but you still need to use mv $f $(dirname $f)/notes.txt to rename file.
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
