How to batch rename a file of specific file type within subdir? - windows

I'd like to batch rename all files within a specific subdirectory. Why does the following give a syntax error?
RENAME .\webapps\*.war .\webapps\Test.war

You can't rename with a target name that includes a path.
RENAME .\webapps\*.war Test.war
should work.

There is some confusion in your statement: Do you want to change all the files to the same name? It's impossible.
And you might use rename the wrong way, please refer to man rename.
NAME
rename - renames multiple files
SYNOPSIS
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
DESCRIPTION
"rename" renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is
expected to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not
be renamed. If no filenames are given on the command line, filenames will be read via standard input.
For example, to rename all files matching "*.bak" to strip the extension, you might say
rename 's/\.bak$//' *.bak
To translate uppercase names to lower, you'd use
rename 'y/A-Z/a-z/' *
OPTIONS
-v, --verbose
Verbose: print names of files successfully renamed.
-n, --no-act
No Action: show what files would have been renamed.
-f, --force
Force: overwrite existing files.
This post might also be useful.

Related

Use grep to exactly match a string in text inside files and then move those files to a separate directory

I have several files inside a directory, some which contain the word "sweet". I would like to use grep to find the files which contain the exact word and then move them to a different folder.
This is my code :
mv `grep -lir 'sweet' ~/directory1/` ~/directory2
However, there are some files with the word "sweets" or "sweeter" or "Sweet", my command is moving them as well, whereas I want the match to be strictly "sweet".
Please help, thanks.
Using grep -lrwF works
check comment thread with Shawn

bash cat all files that contains a certain string in file name

In bash, how would you cat all files in a directory that contains a certain string in its filename. For example I have files named:
test001.csv
test002.csv
test003.csv
result001.csv
result002.csv
I want to cat all .csv that contains the string test in the file name together, and all .csv that contains the string result in the file name together.
Just:
cat *test*.csv
cat *result*.csv
For all files with test (or in case of the second one result) in their name.
The shell itself can easily find all files matching a simple wildcard.
cat *test*.csv >testresult
You want to take care so that the output file's name does not match the wildcard. (It's technically harmless, but good practice.)
The shell will expand the wildcard in alphabetical order. Most shells will obey your locale, so the definition of "alphabetical order" may depend on current locale settings.
Here's very simple way
cat `find . -name "*test*.csv"`

what does the dollar sign mean in the substitution line of bash command "rename"

I was reading about rename and came across this example to change the file extension from htm to html:
rename -v 's/\.htm$/\.html/' *.htm
and it said: The $ means the end of the string. \.htm$ means that it will match .htm but not .html.
I was a bit confused by the use of $ here. Since we already specified *.htm at the end of command line, rename would only select out the htm files (instead of html). So why was it necessary to use the $ still? In another words, what's wrong with no using $?
Anchor $ matches the end of the source file name and it is still required in your regex and dot should also be escaped otherwise abc.htm.htm will be renamed to abc.html.htm instead of abc.htm.html.
Correct command is:
rename -v 's/\.htm$/.html/' *.htm

How to delete files like 'Incoming11781rKD'

I have a programme that is generating files like this "Incoming11781Arp", and there is always Incoming, and there is always 5 numbers, but there are 3 letters/upper-case/lower-case/numbers/special case _ in any way. Like Incoming11781_pi, or Incoming11781rKD.
How can I delete them using a script run from a cron job please? I've tried -
#!/bin/bash
file=~/Mail/Incoming******
rm "$file";
but it failed saying that there was no matching file or directory.
You mustn't double-quote the variable reference for pathname expansion to occur - if you do, the wildcard characters are treated as literals.
Thus:
rm $file
Caveat: ~/Mail/Incoming****** doesn't work the way you think it does and will potentially match more files than intended, as it is equivalent to ~/Mail/Incoming*, meaning that any file that starts with Incoming will match.
To only match files starting with Incoming that are followed by exactly 6 characters, use ~/Mail/Incoming??????, as #Jidder suggests in a comment.
Note that you could make your glob (pattern) even more specific:
file=~/Mail/Incoming[0-9][0-9][0-9][0-9][0-9][[:alpha:]_][[:alpha:]_][[:alpha:]_]
See the bash manual for a description of pathname expansion and pattern syntax: http://www.gnu.org/software/bash/manual/bashref.html#index-pathname-expansion.
You can achieve the same effect with the find command...
$ directory='~/Mail/'
$ file_pattern='Incoming*'
$ find "${directory}" -name "${file_pattern}" -delete
The first two lines define the directory and the file pattern separately, the find command will then proceed to delete any matching files inside that directory.

Way to move files in bash and rename copied file automatically without overwriting an existing file

I'm doing some major restructuring of large numbers of directories with tons of jpgs, some of which my have the same name as files in other directories. I want to move / copy files to alternate directories and have bash automatically rename them if the name matches another file in that directory (renaming IMG_238.jpg to IMG_238_COPY1.jpg, IMG_238_COPY2.jpg, etc), instead of overwriting the existing file.
I've set up a script that takes jpegs and moves them to a new directory based on exif data. The final line of the script that moves one jpg is: mv -n "$JPEGFILE" "$DIRNAME"
I'm using the -n option because I don't want to overwrite files, but now I have to go and manually sort through the ones that didn't get moved / copied. My GUI does this automatically... Is there a relatively simple way to do this in bash?
(In case it matters, I'm using bash 3.2 in Mac OSX Lion).
This ought to do it
# strip path, if any
fname="${JPEGFILE##*/}"
[ -f "$DIRNAME/$fname" ] && {
n=1
while [ -f "$DIRNAME/${fname%.*}_COPY${n}.${fname##*.}" ] ; do
let n+=1
done
mv "$JPEGFILE" "$DIRNAME/${fname%.*}_COPY${n}.${fname##*.}"
} || mv "$JPEGFILE" "$DIRNAME"
EDIT: Improved.
You can try downloading and seeing if Ubuntu/Debian's Perl-based rename works. It has sed-style functionality. Quoth the man page (on my system, but the script should be the same one as linked):
"rename" renames the filenames supplied according to the rule specified
as the first argument. The perlexpr argument is a Perl expression
which is expected to modify the $_ string in Perl for at least some of
the filenames specified. If a given filename is not modified by the
expression, it will not be renamed. If no filenames are given on the
command line, filenames will be read via standard input.
For example, to rename all files matching "*.bak" to strip the
extension, you might say
rename 's/\.bak$//' *.bak
To translate uppercase names to lower, you'd use
rename 'y/A-Z/a-z/' *

Resources