Hot answers tagged

4

Two changes to your current script: don't parse ls; instead rely on the shell's globbing because the files are in a subdirectory, either cd there first and run the loop, or use basename and dirname to pull out the directory and filename portions of the file before adding the prefix. (Note: I also changed your "/Path" to "./Path" as I didn't want to ...


4

Use perl rename. Firstly use the -n flag for a dry-run. rename -n 's/^(...)_(..._.)/$2_$1/' * Then, if you are happy, run it for real. rename 's/^(...)_(..._.)/$2_$1/' * Explanation This uses capturing groups. rename 's/foo/bar/' *: replace foo with bar for all files *. ^(...)_(..._.): from the beginning of the line ^, capture the first three ...


3

In the simple case you show above, where each line has two "words", the target directory and the new name, and where neither can contain any whitespace, you can simply do: while read -r from to; do mv "$from" "$to"; done < file That will read each line in file assign the first string (until the first whitespace) to $from and the rest of the line to $...


3

This is how you can change the name: cd /home/steam/SteamCMD/ mv \~ new_name


2

Assuming GNU tools, I'd do something like find . -type f \( -name '*.[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9].log.*.gz' \ \( -mtime -10 -o -delete -o -true \) -o -name '*.log.*' ! -name '*.gz' \ \( -mtime +9 \( -delete -o -true \) -o -printf '%TF/%p\0' \) \) | while IFS=/ read -rd '' date file; do basename=${file##*/} dirname=${...


2

#!/bin/bash workdir="/tmp/myfilestomove" #cd "$workdir" [ ! -e "$workdir" ] && exit find -type f "$workdir" | grep -v '.gz' | while read file; do filedate="$(date -r "$file" +%d%m%Y)" basename="${file%.*}" extension="${file##*.}" echo mv "$file" "${basename}.${filedate}.${extension}" echo gzip "${basename}.${filedate}.${...


2

Assuming the perl rename command: You're quite close with the last command. rename 's/(0.) /$1 - /' *.mp3 would work. There's no need to escape the space, they have no special meaning in regular expressions (they do in file names, but that doesn't matter here), and you need parentheses around the part you want to reuse.


2

The find command allows you to limit what files are matched. You can then call your script with the exec option e.g. find . \( -name '*.mp3' -o -name '*.avi' \) -exec /path/to/your/script.sh Now your script will be called for each mp3/avi file in the tree. A simple test to show this would be to have script.sh read something like #!/bin/bash echo ...


2

$ ls -1 file1-music-mozart-directed-by-karajan-youtube.mp3 file2-music-beethoven-symphonies-youtube.mp3 $ rename -v -e 's/youtube/YT/; s/(-music-)([[:alpha:]])/$1 . uc $2/e; s/(-directed-by-)([[:alpha:]])/$1 . uc $2/e' * file1-music-mozart-directed-by-karajan-youtube.mp3 renamed as file1-music-Mozart-directed-by-Karajan-YT.mp3 file2-music-beethoven-...


2

Well, if the date strings are in the file names and all the files are in the same directory, you could do: mv 201601*.txt 2016/January Doing this 12 times manually would be a pain, so I would create a list with the number and corresponding month name: $ paste <(printf '%s\n' {01..12}) <(cal 2016 | grep -Po '\s+\K[A-Z]\w{2,}') 01 January 02 ...


1

$ rename 's/^(\d\d)\s*/$1 - /' *.mp3 This will rename all MP3 files that has a double digit at the start of their file names, inserting space-dash-space after the digits. So 01 Track name.mp3 will become 01 - Track name.mp3 Judging from your own attempts, all filenames start with the digit zero, and you appear to want to insert a dash directly after the ...


1

Thanks for @SiyuanRen 's suggestion. convmv can deal with the mess situation keeping ascii unchanged which avoid being garbled. Command convmv -f gbk -t utf8 * works fine under this circumstance. By the way, another solution is use -o loop,utf8 while mounting image files, or just use udisksctl which can automatically deal with filename encoding. P.S. the ...


1

Does it have to use the rename command? $ ls 01 Track name.mp3 02 Track name.mp3 03 Track name.mp3 $ for a in *.mp3 > do > mv -i "$a" "${a%% *} - ${a#* }" > done $ ls 01 - Track name.mp3 02 - Track name.mp3 03 - Track name.mp3


1

That's the Perl rename, I suppose. Perhaps something like this would work: rename 's/^(\d+) ([^-])/$1 - $2/' [0-9]*.mp3 Match anything starting with numbers, then a space, then something other than a dash. Replace with the numbers, a dash, and the next character. (The rest of the name is not touched.) Explicitly checking for the dash here so repeated ...


1

Zsh has a nice function to rename files based on name patterns: zmv. In the replacement pattern, $f designates the whole original name and $1, $2 etc. are the parenthesized groups. Use an array to store month names. autoload -U zmv months=(January February March April May June July August September October November December) zmv '(2016)(<1-12>)*.txt' '...



Only top voted, non community-wiki answers of a minimum length are eligible