Tag Info

New answers tagged

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.


25

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 () { ...


-1

@jordanm and @Gilles pointed out my other answer was non-functional because of the expansion I was attempting within the alias. I put some thought into this answer, and verified it works as expected. Add this function to your ~/.bashrc: swap(){ [ -e $1 ] && [ -e $2 ] temp=$(date +%s) mv -f $1 $temp mv -f $2 $1 mv -f $temp $2 } ...


2

There is difference between renaiming and moving to somewhere. In the case easyest way (in modern bash) is loop through all files: for f in *.* do d=${f::1}/${f:1:1} [ -d "$d" ] || mkdir -p "$d" mv "$f" "$d" done


0

If you use IBM AIX you won't have a rename command, so in order to batch remove file extensions you'll have to use plain vanilla System V UNIX commands: for file in *.tif; do mv $file `echo $file | sed 's/.tif$//'; done;


1

Fixed script: for file in /home/pintolcv/Downloads/2014*D890.E.m; do filepath=${file%/*} filebasename=${file##*/} #echo "File equals: $file" #echo "File path equals: $filepath" #echo "File basename equals: $filebasename" if [[ -e "$file" ]]; then echo "File Found: $filebasename - ${filebasename:5:3}" mv "$file" ...


0

In case you want to know what went wrong in your version: You used $files instead of $x in the basename command. So this should work (untested, though): for x in *.txt do mv "$x" "`basename '$x' .txt`.text" done


0

rename "s/oldExtension/newExtension/" *.txt Above works fine but limited to current directory. Try the command below, which is flexible with sub-directories. It will rename all .txt files under directory structure with a new extension. find . -name "*.txt" -exec rename 's/.txt$/.newext/' {} \;


1

If there are only files which shall be renamed: for file in *; do mv "$file" "${file}.csv" done If there are files with a dot which must be excluded: for file in *; do [[ $file == *.* ]] && continue mv "$file" "${file}.csv" done Or with shopt -s extglob: for file in +([^.]); do mv "$file" "${file}.csv" done


0

I'm not entirely sure why you're using cat FIM_1.txt as this is a file you've not told us about. This code will copy file.txt from each of a set of folders and place in in the directory X named as the folder from which it came. cd "$insub_FIM_M_1" for DIR in * do test -d "$DIR" -a -f "$DIR/file.txt" || continue mv -f "$DIR/file.txt" ...


0

I tried this code and it worked! insub_FIM_M_1=path to a parent folder conatin all the folders (A,B,C,D,E,F,G,H) out=path #!/bin/bash for i in $(cat $insub_FIM_M_1/FIM_1.txt); do cp insub_FIM_M_1/file.txt ${path}/${i}.txt done



Top 50 recent answers are included