Tag Info

Hot answers tagged

10

Use this with bash: find $1 -name "* *.xml" -type f -print0 | \ while read -d $'\0' f; do mv -v "$f" "${f// /_}"; done find will search for files with a space in the name. The filenames will be printed with a nullbyte (-print0) as delimiter to also cope with special filenames. Then the read builtin reads the filenames delimited by the nullbyte and ...


8

Using rename find . -type f -name "* *.xml" -exec rename "s/\s/_/g" {} \; or with $1 find "$1" -type f -name "* *.xml" -exec rename "s/\s/_/g" {} \; Using mv find . -type f -name "* *.xml" -exec bash -c 'mv "$0" "${0// /_}"' {} \; or with $1 find "$1" -type f -name "* *.xml" -exec bash -c 'mv "$0" "${0// /_}"' {} \;


5

If you've a snapshot of the filesystem, or a backup, then yes. If you haven't, then no.


3

If your rename is the Perl one: rename 's/[\/\\?*:><|"]//g' *.extension


3

Here's one simple way, that addresses your original question completely. Could be done as a one-liner, but this syntax keeps it readable. #!/bin/bash for F in "$@" do echo mv "$F" "${F%.pdf}[$(pdfinfo "$F" | awk '/^Pages/{print $NF}')].pdf" done $ ls *pdf aosa-bash.pdf article.pdf bash.pdf bashref.pdf rose94.pdf $ find . -name \*.pdf -exec ./pdf.sh ...


2

With bash: for f in XYZ*/*; do mv -v "$f" "${f%/*}/${f:0:5}${f##*/}"; done The for loop runs trough all XYZ* directories. Then the mv command renames the files. Where: $f is the original filename ${f%/*} is the directory name ${f:0:5} is the prefix ${f##*/} is the original filename


2

With zsh: autoload zmv # best in ~/.zshrc zmv '(XYZ??)(*)/(*)' '$1$2/$1$3'


2

Use this: c=132 for f in *; do mv -v "$f" "enum-$(printf '%0*d' 5 $c)" c=$(($c+1)) done The c=<your_starting_number>; I assumed 132 as in your question. Then the for loop runs trough all the files in the current directory. For every file the mv command is called. the printf utility prints the new filename with leading zeros. And finally the ...


2

Use this: find -name "* *" -print0 | sort -rz | \ while read -d $'\0' f; do mv -v "$f" "$(dirname "$f")/$(basename "${f// /_}")"; done find will search for files and folders with a space in the name. This will be printed (-print0) with nullbytes as delimiters to cope with special filenames too. The sort -rz reverses the file order, so that the deepest ...


1

You can use prename (rename) perl script comes with perl package in Debian/Ubuntu. It is actually a fork of the original rename script. To solve your issue you can do: rename -n 's/^([^[:digit:]]+)([^.]+)/$2\.$1/' *.doc -n is for dry-run, if you are satisfied with the changes remove -n. Test : $ rename -n 's/^([^[:digit:]]+)([^.]+)/$2\.$1/' *.doc ...


1

So, here are two scripts, which you can put in a folder with the pdf-files needing to be renamed. The first one is for adding page numbers and the second is for deleting them. Both scripts are interactive Type abort Enter to exit the script, type n Enter to enter interactive file-by-file mode and if you want a script to proceed with all pdfs without ...


1

For mass renaming, prename is your friend. In this case: prename 's/^(\d*)-(\d*-\d*)-(\d*)-.*___\d*_(\w*)___\w*_(\w*)_\w*/$1.$2.$3.$4.$5/' * (ignoring the Julian date until the respective questions have been clarified).


1

You can use parameter expansion mechanism shopt -s globstar for file in **/*foo.bar; do prefix="${file%.foo*}" suffix="${file##*.foo}" mv -v -- "$file" "$prefix$suffix" done The ${file%.foo*} removes matching suffix (leaving only prefix), and ${file#*.foo} removes prefix (leaving suffix). The double star glob (**) is needed to traverse all ...


1

By using \0-delimited strings, this can handle spaces and \n in file names. cd "${PROJECT_DIR%/*}" outdir="output"; mkdir -p "$outdir" find "$PROJECT_DIR" -type f -name '*.log' -printf "%p\0${outdir}/%P\0" | awk 'BEGIN{FS="/";RS=ORS="\0"} NR%2||NF==2 {print; next} {gsub("/","#"); sub("#","/#"); print}' | xargs -0 -n2 cp -T mkdir -p ...


1

#!/bin/bash newdir=/absolute/path/output olddir=/absolute/path/project find $olddir -name '*log' | while read line ; do if [ "$olddir" == "$( basename "$line" )" ] ; then #just move the file if there are no subdirectories mv "$line" "$newdir" else #1) replace old project dir with nothing #2) replace all slashes with hashes #3) set ...


1

Assuming none of your files have spaces: for i in *k120*; do mv -- "$i" "$i.dat" done


1

No need for find, it works with pure bash, considering the following directory structure: ./folder3 ./folder3/folderc ./folder2 ./folder2/folderb ./folder1 ./folder1/foldera The command: for f in folder*/*/; do mv -v "$f" "${f%???}"; done After: ./folder3 ./folder3/folde ./folder2 ./folder2/folde ./folder1 ./folder1/folde The filename expansion ...


1

The trick is to use the mindepth and maxdepth argument Here is the command that I used: `find folder -mindepth 2 -maxdepth 2 -type d -exec sh -c 'mv "${0}" "${0%?}"' {} \;` Also note that I used ${0$?} instead of the double ?? that you used. One way to debug find problems like these is to apend your action with echo. So in the above example you would ...



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