Tag Info

New answers tagged

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


0

This is one of those few cases where the rename utility from the util-linux suite is up to the task. Most distributions ship this utility as rename; on Debian, Ubuntu and derivatives, rename is a different, more powerful utility and the util-linux utility is called rename.ul. rename 'Garbage ' '' *.txt A fully portable solution is for x in *.txt; do mv ...


0

Just using bash: for file in ./*.txt; do mv -- "$file" "${file/Garbage /}"; done


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


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


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


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.


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


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


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


0

With this awk program you can create the shell commands and, in case of doubt, inspect in advance whether they are correct... awk -v n=5 '{ printf "mv \"%s\" %s\n", $0, "direc" int((NR-1)/n)+1 }' list If you are okay with the output pipe the whole command into sh. Also, if you want to avoid the extra file 'list' you can create it on the fly; the whole ...


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


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


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


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


0

Found two ways: for file in /src/*.desktop.in; do file=${file%.in} if test -e "/dest/$(basename ${file})" then cp "/src/${file}.in" "/dest/${file}" fi done rsync and --existing: for file in /src/*.desktop.in; do rsync --dry-run --existing --verbose "/src/${file}" "/dest/${file%.in}" done


0

What you have is basically as good as it gets. You can save a tiny bit of file name manipulation by changing to the directory where you're enumerating the files. It's a matter of readability, not performance. set -e cd /destination for file in *.desktop; do cp "/src/$file.in" "$file" done Don't forget to check for failures.


0

Not necessarily more elegant, but for completeness, here's a sed solution: for file in *; do mv "$file" "$(echo "$file" | sed 's/^\(.*\)\.\?.*$/\1.txt/')" done The sed bit grabs everything before the last dot in the filename, and replaces the dot and whatever follows with '.txt'. If there is no dot in the filename, then '.txt' is just appended to the ...


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


0

To rename all of them in the shell would be something like this: for file in *; do mv -- "${file}" "`basename -- "${file}"`.txt" done You may want to test the type with file or other checks to determine the extensions.


0

And it depends on how you define “changing the current directory”. /tmp/test$ (cd ..; mv test test2) /tmp/test$ pwd /tmp/test /tmp/test$ pwd -P /tmp/test2 spawns a subshell and changes the current directory in the subshell, but leaves your primary shell where it was.


1

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


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


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


0

Here a basic way to do something like this using a shell script and awk. This example script is fairly explicit for your particular case, its using the the second field in the file-name is a valid integer. And trusts that there no stray underscores in the file-names. A more general purpose script would validate that field 2 is just numbers and make the ...


2

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


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


0

Here's what I ended up using : file1=1stfile file2=2ndfile temp="$(mktemp -dp /mnt/sdcard)" mv "$file1" $temp mv "$file2" "$file1" mv $temp/"$file1" "$file2"


2

There's no low-level way to swap files, so you need to use an intermediate temporary name. For robustness, make sure that the temporary name won't be used by any other program (so use mktemp) and that it's on the same filesystem as one of the files (otherwise the files would be needlessly copied instead of being just renamed). swap_files () { ...



Top 50 recent answers are included