Tag Info

Hot answers tagged

31

Yes, but you have to refer to the directory by name, not by using the . notation. You can use a relative path, it just has to end with something other than . or ..: /tmp/test$ mv ../test ../test2 /tmp/test$ pwd /tmp/test /tmp/test$ pwd -P /tmp/test2 You can use an absolute path: /tmp/test$ cd `pwd -P` /tmp/test2$ mv $PWD ${PWD%/*}/test3 /tmp/test2$ ...


3

Using the utility rename from util-linux, which CentOS 6 provides, and assuming bash: rename $'\n' '' wget_* This asks to delete newline characters from the names of listed files. I recommend trying it out on a small subset to ensure it does what you want it to (note that rename on CentOS 7 supports a -v switch to show you what changes it is making). If ...


2

If there is no extention in files name: for i in secondfolder/IM_* ; do mv "$i" "${i%_[0-9]*}_$[10#${i##*_}+2048]" ; done


2

Here's a simple POSIX shell solution that loops over the files, parses the directory parts of the names and renames them one by one. for x in ccc/*/* ddd/*/* lll/*/*; do dir2=${x%/*}; dir1=${dir2%/*}; dir2=${dir2#"$dir1"} mv -- "$x" "${x%/*}/${dir1}_${dir2}_${x##*/}" done In bash, you can use the parameter expansion string replacement feature to avoid ...


2

In bash: for x in ccc/*; do [ -f "$x" ] || continue # skip directories in ccc y=${x#*_} # strip the ccc_ prefix cp -p ddd/*/ddd_"${y/_c_/_d_}" ddd/ # copy the corresponding file under ddd cp -p lll/*/lll_"${y/_c_/_l_}" lll/ # ditto under lll done This script assumes that you don't have e.g. ...


2

On Debian, Ubuntu and derivatives, you can use the rename Perl script: rename 's/(?<=-)([0-9]+)/sprintf "%03d", $1/e' prefix-*.ext Some systems may have this command installed as prename or perl-rename. Note that this is not the rename utility from the util-linux suite which does not provide an easy way to do this. In zsh, you can use zmv to rename ...


2

You can do it with rename from util-linux too: rename vocabulaire_ vocabulaire_0 *vocabulaire_[0-9]_*.mp3 result: pro2e_u01_txt_vocabulaire_01_campus.mp3 pro2e_u01_txt_vocabulaire_02_personnes.mp3 pro2e_u01_txt_vocabulaire_12_nationality.mp3 pro2e_u01_txt_vocabulaire_231_whatever.mp3 if you wish to further zero-pad repeat the command increasing the ...


2

list=(*) # an array containing all the current file and subdir names nd=5 # number of new directories to create ((nf = (${#list[@]} / nd) + 1)) # number of files to move to each dir # add 1 to deal with shell's integer arithmetic # brace expansion doesn't work with variables due to order of expansions echo mkdir ...


1

On Linux, you can use cp --backup=numbered to obtain files called foofile.log, foofile.log.~1~, foofile.log.~2~, etc. find /location -name '*file.log' -exec cp --backup=numbered {} /location \; If you want to include the original directory as part of the target file name, you can replace the slashes by some other string. However, there are a couple of ...


1

With perl's rename : rename -n 's/.*\s+//' *.txt remove -n switch to do it for real. (-n == dry-run)


1

for f in ./*.mp3 do set "${f%_*}" "_${f##*_}"; f=10${1##*_} mv "$1$2" "${1%_*}_${f#*$((${#f}<4))}$2" done That should work, I think, so long as your number field is between the last and second-to-last _ divided field and given you only wish to pad to 2 digit places.


1

Sometimes a specific solution is good enough; like in the current case where you can identify the patterns for the file subset to consider (e.g /_[0-9]_/) and add a leading zero depending on a uniquely identifying prefix (e.g. /re_/). Put all together that would be: for f in *_[0-9]_*.mp3 ; do mv -i "${f}" "${f/re_/re_0}" ; done For the pre-check you ...


1

On Debian, Ubuntu and other systems where rename is a Perl script (as opposed to the one in util-linux), this command will be sufficient as long as the files are from 1 to 99. rename 's/_vocabulaire_([0-9]{1})_/_vocabulaire_0$1_/' *.mp3 If you want to just do a dry-run without renaming the files pass in the -n option. It will show what will actually be ...


1

So, given the limited info you've given, let me state my assumptions which, if any are wrong, let me know and I/we can adjust the answer. The assumptions are critical to keeping the logic short and sweet, otherwise you do descend into 'overkill' mode. Assumption 1. The 'suffix' for your file is what you want changed, and the 'suffix' is the number(s) after ...


1

for file in prefix-*.ext do num=`echo $file | sed 's/prefix-\(.*\).ext/\1/'` [ $num -lt 100 ] && mv $file prefix-`printf "%03d" ${num}`.ext done


1

Perhaps something like this (untested) for n in *.ext; do echo mv "$n" \ "prefix-$(printf %03d "$(basename ${n#prefix-} .ext)").ext" done Remove the echo to do the actual renaming. Note: you have to be in the directory where the files are for this method to work; dealing with pathnames that contain a directory part is more complicated.


1

#!/bin/sh d=1 # index of the directory f=0 # number of files already copied into direc$d for x in *; do # iterate over the files in the current directory # Create the target directory if this is the first file to be copied there if [ $f -eq 0 ]; then mkdir "direc$d"; fi # Move the file mv -- "$x" "direc$d" ...


1

d=0; set ./somedirname #init dir counter; name for f in ./* #glob current dir do [ -f "$f" ] && set "$f" "$@" && #if "$f" is file and... [ "$d" -lt "$((d+=$#>5))" ] || continue #d<(d+($#>5)) or continue mkdir "$6$d" && mv "$@$d" || ! break ...


1

You could do something like this: for dir in ccc ddd lll; do find "main/$dir" -type f -print0 | while IFS= read -r -d '' f; do dd=$(dirname "$f") new="${f/main\/}" new="${new//\//_}" mv "$f" "$dd"/"$new" done done After the above script, your files will look like this: . `-- main ...


1

Believe this should work for you (probably more elegant ways to do this with sed). for file in *; do base=`echo "${file%.*}"` mv -- "${file}" "${base}.txt" done


1

for file in /destination/*.desktop; do echo cp "/src/${file##*/}.in" "$file"; done If everything looks good, remove echo.


1

If you want to avoid re-inventing the wheel, you could use the mv command's built in ability to do automatic numbered backups; if your shell supports the case conversion natively that could be as simple as for f in *; do mv --backup=numbered -- "$f" "${f,,}"; done The default backup number format is .~1~, for example given SOME FILE sOmE fIlE some file ...


1

Simple way in bash: for i in $(ls /path/to/your-directory); do name=$(echo "$i" | cut -d '_' -f1) num=$(echo "$i" | cut -d'_' -f2) num1=$(( num + 2048 )) mv "$i" "${name}_$num1" done



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