New answers tagged

0

I would do it like this: for f in [a-z][0-9].txt.gz; do mv $f "${f:0:1}0${f:1}" done I adapted this from something else (which used awk because it needed more, but thanks to a comment I made it better).


0

Based on Padding a number in a filename to a fixed length, I found this to work for me. This is a hacky way and I am surprised there is no proper/simple way to do this and without additional programs or functions. var=$(find . | grep -E "[a-z][0-9]{1}\.+" | sed 's/\.\///') for f in $var; do alpha=$(basename $f .gz | cut -c1-1) num=$(basename $f .gz |...


0

This would do it: find . -name '??.txt.gz' | sed -e 's/..\(.\)\(.*\)/mv \1\2 \10\2/' > /tmp/cmds sh /tmp/cmds The find command gets the list of files that need to be renamed. The sed command constructs a list of mv commands to do what you want. Those get written to a file and then executed as a shell script. I'm sure there's a way to do this as a one-...


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 ...


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 ...


0

There are two (three) erase commands: one that's part of the util-linux package that's installed on every non-embedded Linux system, and one (two variants actually) based on Perl. See What's with all the renames? The util-linux command is very basic, but you're in the rare situation where it can do what you want. Replace the first space by space-dash-...


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 ...


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.


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' '...


0

Here is a bash solution that runs through the files, moving those that match the yyyymm component (for values of yyyy in the range 2000-2099): months=('' January February March April May June July August September October November December) for f in 20[0-9][0-9][0-3][0-9]* do year=$(echo "$f" | grep -Po '^20\d\d') # Extract the four digit ...


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

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 ...


0

The regular expression match /^(.*) - (.*) - (.*) part([0-9]+)/ stuffs the name, episode indicator, title and part number in groups which you can then use as $1 through $4 in the replacement text. rename 's/^(.*) - (.*) - (.*) part([0-9]+)/$1 - $2.$4 - $3/' * Anything after the part1 part, such as a file extension, is left unchanged. If you want to ...


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-...


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 ...


0

I've found something but doesn't work well: find /var/www/domain.com -type f ! \( -name '*.db' -o -name '*.sqlite' -o -name '*.feed' -o -name '*.com' -o -name '*.xml' -o -name '*.gz' -o -name '*.txt' -o -name '*.pdf' -o -name '*.js' -o -name '*.css' -o -name '*.ico' -o -name '*.gif' -o -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) -maxdepth 1 -exec ...


0

Ideally, this should be done in a language like perl or python which have good HTML parsing libraries. But if you want to do it in a shell script, you can install the xml2 package, which provides tools called html2 and 2html for converting HTML to and from a flat-file format suitable for use with line-oriented tools like sed, grep, awk, etc. It also ...


0

To escape # from the shell, just use single quotes ('#'), double quotes ("#"), or backslash (\#). The simplest in your case would be to use the rename command (if it is available): rename '#U00a9' safe *.jpg


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 $...



Top 50 recent answers are included