Tag Info

Hot answers tagged

4

No need for bash here, any standard sh interpreter implementation will do: #! /bin/sh - ret=0 for file do dir=$(dirname -- "$file") case $dir in (*[!/]*) dir=$dir/ # handle / and // specially esac base=$(basename -- "$file") name=${base%.*} name=${name:-$base} # don't consider .bashrc the extension in /foo/.bashrc ext=${base#"$name"} ...


3

Perhaps like this: for f in /some/dir/*; do type="$( file -bi -- "$f" )" case "${type%%;*}" in image/jpeg) ext=jpg ;; image/png) ext=png ;; image/tiff) ext=tiff ;; *) printf '%s: %s: unknown file type\n' "${0##*/}" "$f" >&2; ext='' ;; esac if [ -n "$ext" ]; then mv -n -- "$f" "${f}.${ext}"; fi done


2

This script ought to do it: #!/bin/sh for f in Dorn_Triatom* ; do mv "$f" `echo "$f" | sed -e 's/Dorn_Triatomine_//' -e 's/sequence_1_unmappedforTdim_//'` done


2

On Linux, assuming that none of your file names have _create inside them (only at the end), you can use the rename utility from the util-linux package, which is installed on all non-embedded Linux systems. Call find to execute the command on all files in subdirectories recursively. find -depth -exec rename _create _bak {} + On Debian and derived ...


1

On Debian, Ubuntu and derivatives, with the Perl rename, sometimes installed as prename: #enable ** globbing shopt -s globstar #use the rename utility rename -n 's/_create$/_bak/' **_create Remove the -n (= --no-act) flag if it works as expected. Note: This is probably the shortest efficient way to do this, especially if you've got shotp -s globstar ...


1

Another way with exiftool: exiftool -r -ext '' '-filename<$filename.$filetype' . this recursively renames all image files without extension under current directory . to filename.TYPE where TYPE is JPEG, PNG, TIFF etc. Or, to rename only files of type JPEG/PNG/TIFF that have no extension: exiftool -if '$filetype eq "JPEG"' -filename=%f.jpg \ -execute ...


1

#!/bin/bash ss=0 for file do cp -fp -- "$file" "${file%.*}_copy.${file##*.}" || ss=$? done exit $ss This fails if file does not have a dot extension part. If you need that to work use St├ęphane Chazelas's solution.


1

Since the OP is asking for a bash solution. Here is one that does. #!/bin/bash if [[ ! -f $1 && $(($# != 1)) ]]; then printf '%s\n' "Provide a filename" exit 1 fi inFile="$1" fileExt="${1#*.}" destFile="${1%.*}" cp -- "$inFile" "${destFile}_copy.$fileExt" # As suggested, so the files that start with a dash are not ignored.


1

With bash and find find . -type f -name accepted_hits.bam -exec bash -c \ 'i=0; for f; do (( ++i )); mv -- "$f" "${f}_$i"; done' _ {} +


1

You can use rename: find /var/www/vhosts/domain.com/httpdocs/ -type f -name "*.*~" -exec rename 's/~$//' {} \;


1

find /var/www/vhosts/domain.com/httpdocs/ -type f -name '*.*~' \ -exec sh -c 'for arg do mv -- "$arg" "${arg%\~}"; done' _ {} + Passing _ first sets $0 to _, which would otherwise have taken your first filename. Using + instead of ; tells find to pass as many filenames as it can to a single command instead of executing a new shell for each file.


1

find is rarely useful when you don't need to traverse a directory tree recursively. Here a simple loop and shell wildcards are enough. for x in */161901.pdf; do mv -- "$x" "xyz/${x%/*}.pdf" done Or, with the Perl-based rename command on Debian, Ubuntu and derivatives (prename on Arch, not the Linux rename command on other distributiosn): rename ...



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