Hot answers tagged

3

You can use the rename command (see edit 1). Solution 1 For a reasonable number of files/directory, by setting bash 4 option globstar (not works on recursive name, see edit 3): shopt -s globstar rename -n 's/etckeeper/userkeeper/g' ** Solution 2 For a big number of files/directories using rename and find in two steps to prevent failed rename on files ...


3

This does what you ask: n=1; for f in *.txt; do mv "$f" "CO_$((n++))_$f"; done How it works n=1 This initializes the variable n to 1. for f in *.txt; do This starts a loop over all files in the current directory whose names end with .txt. mv "$f" "CO_$((n++))_$f" This renames the files to have the CO_ prefix with n as the counter. The ++ symbol ...


2

You should use -10 and not -1 as argument to head, and you also need quotes around -bkp, so ls -lt *.xls | head -10 | awk '{print "mv " $9 " "$9"-bkp"}' | sh should work. And you would probably have realised if you had tried removing | sh, so the command just ends with awk printing the commands.


2

AFAIK, in this context the {} aren't really "shell curly braces" (in the sense of brace expansion for example), they're just textual placeholders for arguments. As such you could pass them into a shell command and use the shell's parameter substitution facility to modify the names, like sh -c 'for f; do somecommand "$f" "${f%.*}.al"; done' sh {} To ...


2

You want to do the rename backwards: counter=$((final_number + 1)) for index in {final_number..0}; do mv "icon_${index}.icns" "icon_${counter}.icns"; let counter--; done


2

Your regular expression doesn't match the pattern in your filename. To match at least one digit, you need to use [0-9]+ (you can also use \d to match digits); your pattern will only match 1 digit. Your example filename doesn't have spaces around -, but you have them in the pattern. And you're not doing anything to remove the ] at the end. Try: rename ...


2

Using bash's built-in substring expansion: for f in 2015* ; do mv "$f" "${f::4}-${f:4:2}-${f:6}" done


2

With sed: LC_ALL=C sed -e 's/\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1_\2_\3/' <file


2

Here's a fairly simple and straight-forward shell script that uses jsonpipe to do what you want. It doesn't use any fancy sh/bash features, and does only the bare minimum sanity checking of filenames. NOTE: jq is far more capable than jsonpipe, but jsonpipe is simpler and easier to use when you don't particularly care (or want to know) about the structure ...


1

#!/bin/bash workdir="/tmp/myfilestomove" #cd "$workdir" [ ! -e "$workdir" ] && exit find -type f "$workdir" | 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}.${extension}" done ...


1

What you seem to want is a non-greedy match up to and including a closing parenthesis. You should also escape the period if you want it to be treated as literal: $ rename -n 's/\(feat\..*?\)//' *.mp3 103 Flume - Left Alone (feat. Jezzabell Doran).mp3 renamed as 103 Flume - Left Alone .mp3 214 Flume - Sleepless (feat. Jezzabell Doran) (Shlohmo Remix).mp3 ...


1

The command is like sed, which allows you to specify multiple substitutions using a -e option, e.g., rename -n -e 's/\(feat.//' -e 's/ - //' *.mp3 and get '103 Flume - Left Alone (feat. Jezzabell Doran).mp3' would be renamed to '103 FlumeLeft Alone Jezzabell Doran).mp3' '214 Flume - Sleepless (feat. Jezzabell ...


1

If you don't want to make any assumption on what character filenames may contain, you could do: ls -dt ./*.xls | awk -v q="'" -v n=10 ' function process() { if (NR > 1) { gsub(q, q "\\" q q, file) print "mv " q file q, q file "-bkp" q if (!--n) exit } } /\// { process() file = $0 next } {file = file "\n" ...


1

While rename is a very powerful tool, I normally prefer the simplicity of the mmv (multiple move) utility: mmv '????????_*' '#1#2#3#4-#5#6-#7#8_#9' The ? in the search pattern stands for a single character, the * for an arbitrarily long sequence of characters. In the replacement pattern, every #<number> stands for a corresponding ? or * in the ...


1

Using the perl rename command (which is completely different to the rename command from util-linux): rename -v 's/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/' 2015* (use -n rather than -v for a dry-run to test the command first). This perl version of rename may be called prename or file-rename on your system. It is far more capable and useful than the util-linux ...


1

Your problems will be solved by the following code: #!/bin/sh i=0 for img in `ls SOURCE_FOLDER_W_GOOD_NAMES/*.png`; do mv $img DESTINATION_FOLDER_W_INCREMENT_NAMES/icon_$i.icns i=$((i+1)); done


1

With the prename utility found on Debian and derivatives or available on other systems by installing the Perl package Unicode::Tussle: prename 's ([^/]*\z) (sprintf("C0_%02d_%s", ++$n, $&))e' england.txt canada.txt france.txt Explanation: for each argument, rename the base name (the longest suffix not containing a slash) to C0_ followed by the counter ...


1

1 For your first step, in your command example, you do not need the * because that is not filtering any file. What you do need is to filter .git, as this: $ find . -name '.git' -prune -o -type f -print That will reject (-prune) any directory exactly named .git and everything inside it. That removes the need of grep -v ".git". The sed could not be ...


1

Both answers say essentially the same thing, but only focus on one aspect of removal. If you have a shell whose working directory is within the renamed/moved directory tree, it will continue to see and use those files until they are actually removed. Because of this, the shell will see the files in varying states of deletion, and consequently, the ...


1

I know this is an old question but i have been searching all night for a similar solution. I found a few helpful tips but they did not do exactly what i needed, so I had to mix and match a few to get the correct outcome I was looking for to simply remove special characters and replace them with a (.) dot for f in *.txt; do mv "$f" `echo $f | sed ...



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