Tag Info

New answers tagged

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


0

Not sure if this answers your question, but if you want to know which image type (PNG, JPEG, etc) is a specific file, you can use the file command: file myimage.xxx myimage.xxx: PNG image data, 837 x 814, 8-bit/color RGB, non-interlaced


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


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

cp /example/directory/file.doc /example/directory/file_copy.doc this specifies the file name and will do what you want


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


0

This is very easy do to with shell globs: Add a counter to the name (accepted_hits.bam.1, accepted_hits.bam.2 and so on): c=0; for f in */accepted_hits.bam; do mv "$f" "$f".$((++c)); done Explanation This simply iterates over all files called accepted_hits.bam that are in subdirectories of the current directory, and saves each of them as $f. The ...


0

If you would just like to name them as unique files, you can use the RANDOM variable as below: find /basedir/ -type f -name "accepted_hits.bam" -exec bash -c 'mv $0 $0.$RANDOM' {} + If you would like to sequentially name them, you can append a counter value at the end of the filename as below: for file in $(find /basedir/ -type f -name ...


0

I would probably use awk, but that's just a matter of taste, not of right and wrong: find . -name accepted_hits.bam | awk '{ print "cp -pi \"" $0 "\" \"" $0 "\"_" NR "; mv -i \"" $0 "\"_" NR " ./bam-files/" }' | tcsh -f where you need to create a directory ./bam-files/ before calling above line, or adjust the directory name. I have intentionally used -p ...


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' _ {} +


0

Assuming your directories are all at the same parent, try this: $ for f in $(ls */accepted_hits.bam); do mv $f $(dirname $f)/$(dirname $f)-$(basename $f); done


0

Using grep with PCRE to get the new file name: #!/bin/bash for file in *.tsv; do newname="$(grep -Po '^[^_]*_[^_]*_\K[^_]*_'<<<"$file")$(grep -Po '.*_\K.*$' <<<"$file")" mv "$file" "$newname" done newname is combined of output of two grep operations: grep -Po '^[^_]*_[^_]*_\K[^_]*_'<<<"$file" will output the A5201_ like ...


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


0

My solution is a simple bash script: #!/bin/bash for file in "$(find /var/www/vhosts/domain.com/httpdocs/ -type f -name '*.*~')" do mv "$file" "$(echo $file | sed -e 's/~//g')" 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.


0

Plain POSIX shell answer: #!/bin/sh for i in */*.log do mv "$i" "${i//\//_}" done moves every file matching */*.log to the same name with / substituted by _.


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


0

Later I found while read -r line very helpful and I've succeed with following command:- find -name 161901.pdf | while read -r line; do mv $line ./xyz/$(echo $line | cut -d "/" -f 2).pdf; done Explanation:- Here find -name 161901.pdf list the founded files (already mentioned in question) which piped to while loop in while path stored in variable line. ...


0

If you have rename / prename command find -name 161901.pdf -exec prename -n 's!/!-!g; s!\W*!xyz/!' {} + and remove -n if you like what you see. (Larry Wall and Robin Barker' prename is installed with perl.deb package; Brian d Foy Unicode::Tussle rename also works)


0

Sweet and simple: #!/bin/bash find . -name 161901.pdf | while read -r line do # this will create new filenames e.g. ./May-2013/161901.pdf will be now May-2013-161901.pdf filename=$(echo $line | grep -oP "\/\K.+$" | sed 's/\//-/g') # now just move the files mv $line xyz/$filename done So, essentially it's just a 2-step process. Create new ...


0

This could be a possible solution. The argument of the script is the directory in which you are searching, and now it is just printing what will do: #!/bin/bash find "$1" -type f -name '161901.pdf' -print0 | while IFS= read -r -d '' f do # get folder name d="$(basename "${f%/*}")" target="${d}.pdf" echo cp $f "xyz/${target}" done


1

One way is to use find -print and pipe the filelist to awk where you can transform it to whatever you want, e.g. a cp command: $ find -name 161901.pdf -print | awk -v TARGET=xyz -F'/' '{ printf "cp %s %s/%s.pdf\n", $0, TARGET, $2; }' cp ./May-June-2011/161901.pdf xyz/May-June-2011.pdf cp ./Nov-Dec-2011/161901.pdf xyz/Nov-Dec-2011.pdf and this can then be ...


3

Perl module Unicode::Tussle comes with a very useful script named rename (which is unfortunate, because the name clashes with the standard rename(1) on Linux). With it, you could do something like this: mkdir xyz find . -name '*.pdf' -print0 | \ rename -0 's!^\.!xyz!; s!/[^/]*\.pdf$!.pdf!' Without Perl, you could still do the same thing with a bit ...


1

Using perl's rename (named prename in Ubuntu) prename 's/^([^_]*).*(_R.*)/$1$2/' *.fastq.gz


3

In bash or POSIX sh: for file in *.fastq.gz; do mv -- "$file" "${file%%_*}_${file##*_}" done


1

With zsh: autoload zmv # best in ~/.zshrc zmv '(*)_*(_R*)' '$1$2'



Top 50 recent answers are included