I am trying to write a bourne-shell script that takes a directory as a parameter and look for images named ixxx.a and rename them to ixxx_a.img where "xxx means the extension number for exemple image files would be named i001.a , i002.a ,i003.a ...)
here what I tried
mv $1/f[0-9][0-9][0-9].a $1/f[0-9][0-9][0-9]_a.img
but it says that the DEST is not a directory.
Any help would be much appreciated. Thanks.
for i in $1/f[0-9][0-9][0-9].a; do
mv $i ${i%.a}_a.img
done
However, this does not consider blank spaces in the file/folder names. In this case you'd have to use while so you get one file name per line (see below for bonus). There are probably dozens of other ways, including rename.
find $1 -maxdepth 1 -type f -name "f[0-9][0-9][0-9].a"|while read i; do
mv "$i" "${i%.a}_a.img"
done
Edit: Perhaps I should explain what I did there. It's called string substitution and the main use cases are these for a variable var:
# Get first two characters
${var:0:2}
# Remove shortest rear-anchored pattern - this one would give the directory name of a file, for example
${var%/*}
# Remove longest rear-anchored pattern
${var%%/*}
# Remove shortest front-anchored pattern - this in particular removes a leading slash
${var#/}
# Remove longest front-anchored pattern - this would remove all but the base name of a file given its path
# Replace a by b
${var//a/b}
${var##*/}
For more see the man page.
Related
I've got a group of files with only one specific extension under one specific directory. How do I check whether I've got at least one of those files. If at least one file with that specific extension is present, then print at least one file matching your extension is present in your directory. If not, then print no files of that specific extension exist in your directory.
Also, when no file of a specific extension is present, I've got the following error message that I'm not able to get rid of, even if I use 2>/dev/null redirection: zsh: no matches found: *.mkv (and hence, behaving differently from Bash)
To disable the error, use the N glob qualifier to enable the NULL_GLOB option, replacing the error with an empty result.
% print *.mkv
zsh: no matches found: *.mkv
% print *.mkv(N)
%
For the rest of the problem, you can capture the result in an array named result, then get the number of matches by checking the size of the array with $#result.
If it would be useful, you can also limit which matches a pattern produces. For example, *.mkv[1,5] produces (at most) the first 5 results. The second number is optional; a single value only provides that one element.
My problem solved as follows:
setopt no_nomatch
if ls *.mkv 1> /dev/null 2>&1; then echo OK; fi
setopt nomatch # Undo the change after evaluation (just in case)
Inspired by:
https://unix.stackexchange.com/a/310553
https://unix.stackexchange.com/a/257141
Good read on the topic:
https://scriptingosx.com/2019/06/moving-to-zsh-part-3-shell-options/
Assuming that the directory you are checking is my_dir:
all_files=(my_dir/*(ND))
if (( #all_files > 0 ))
then
echo at least one entry exists in my_dir
fi
This would also include subdirectories within my_dir. If you are only interested in plain files, do instead a
all_files=(my_dir/*(ND,))
I have a directory which contains multiple file (~1000) with names in the form of ABC.txt, ABC.1.txt, ABC.2.txt, XYZ.txt, XYZ.1.txt and so on.. I want to consider these names based on the first index if we split them by .. For example then names will be read as ABC XYZ and so on.. Then I want to remove those files for which these first index does not exist in an other (reference file). Given the file names mentioned above, let's say my other (reference file) only contains 1 name and that is XYZ. So the files that will be kept in the directory will be XYZ.txt and XYZ.1.txt and everything else which does not have the exact prefix as XYZ will be removed. I said exact because it might happen that there will be a file with name XYZA.txt, so there should be an exact match in order to keep that file.
Can anybody help me with this. Thank you very much.
EDIT: One directory contains all the files: ABC.txt, ABC.1.txt, ABC.2.txt, XYZ.txt, XYZ.1.txt and the reference file is in another directory as file name reference.txt and is a one-column file containing other directory's file (prefix)names as ABC, XYZ, CDE etc..
try
for f in *.*;
do if grep -qF "${f%%.*}" file;
then echo "skip $f";
else echo "rm $f"; fi;
done
searches all files with a dot in file name, extract the prefix until the first dot, compare literally in the file with not to be deleted names, and echo the rm file command. Make sure your file with names doesn't have a dot, otherwise it will be removed as well.
if looks fine remove echo before rm.
I have 3 groups of photos, from 3 different cameras (with time sychronised onboard all cameras) but with different naming schemes (e.g.: IMG_3142.jpg, DCM_022.jpg). I would like to rename every photo file with the following naming convention:
1_yyyy_mm_dd_hh_mm_ss.jpg for earliest
2_yyyy_mm_dd_hh_mm_ss.jpg for next earliest, and so on,
until we reach around 5000_yyyy_mm_dd_hh_mm_ss.jpg for the last one (i.e. the most recent)
I would like the yyyy_mm_dd_hh_mm_ss field to be replaced by the “date and time taken” value for when this photo was taken. Which is saved in the metadata/properties of each file.
I have seen awk used to carry out similar operations but I'm not familiar enough to know how to access the “time taken” metadata, etc.
Also, not that this should make a difference: my computer is a mac.
You can use jhead for this. The command is:
jhead -n%Y_%m_%d_%H_%M_%S *.jpg
Make a COPY of your files first before running it! You can install jhead with homebrew using:
brew install jhead
Or, if you don't have homebrew, download here for OS X.
That will get you the date in the filename as you wish. The sequence number is a little more difficult. Try what I am suggesting above and, if you are happy with that, we can work on the sequence number maybe. Basically, you would run jhead again to set the file modification times of your files to match the time they were shot - then the files can be made to show up in the listing in date order and we can put your sequence number on the front.
So, to get the file's date set on the computer to match the time it was taken, do:
jhead -ft *.jpg
Now all the files will be dated on your computer to match the time the photos were taken. Then we need to whizz through them in a loop with our script adding in the sequence number:
#!/bin/bash
seq=1
# List files in order, oldest first
for f in $(ls -rt *jpg)
do
# Work out new name
new="$seq_$f"
echo Rename $f as $new
# Remove "#" from start of following command if things look good so the renaming is actually done
# mv "$f" $new"
((seq++))
done
You would save that in your HOME directory as renamer, then you would go into Terminal and make the script executable like this:
chmod +x renamer
Then you need to go to where your photos are, say Desktop\Photos
cd "$HOME/Desktop/Photos"
and run the script
$HOME/renamer
That should do it.
By the way, I wonder how wise it is to use a simple sequence number at the start of your filenames because that will not make them come up in order when you look at them in Finder.
Think of file 20 i.e. 20_2015_02_03_11:45:52.jpg. Now imagine that files starting with 100-199 will be listed BEFORE file 2o, also files 1000-1999 will also be listed before file 20 - because their leading 1s come before file 20's leading 2. So, you may want to name your files:
0001_...
0002_...
0003_...
...
0019_...
0020_...
then they will come up in sequential order in Finder. If you want that, use this script instead:
#!/bin/bash
seq=1
for f in $(ls -rt *jpg)
do
# Generate new name with zero-padded sequence number
new=$(printf "%04d_$f" $seq)
echo Rename $f as $new
# Remove "#" from start of following command if things look good so the renaming is actually done
# mv "$f" $new"
((seq++))
done
I have thousands of files inside a directory I need to rename adding a prefix like "th_" so that files will be th_65461516846.jpg
but I can't due to the error "argument too long"
I have used this command
rename 's/^/th_/' *
thanks!
The xargs program is used to break command lines into multiple commands to avoid blowing the shell line length limit. In your case, you'd use:
ls | xargs rename 's/^/th_/'
Which repeatedly executes rename with a portion of the output of ls until the list of files is exhausted. Do be aware this idiom requires special attention if the file names have spaces or other funny characters in them (which I'm assuming isn't so based on your example).
This one did the job
for f in *; do mv "$f" "${f/9/th_}";done
or
for f in * ; do mv $f th_${f#} ; done
I don't know what differs between the 2 but in my case they both work.
I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
FOR %%D IN ("%CD%") DO SET "DirName=%%~nxD"
ECHO %DirName%
This script gets the path from the CD variable and extracts the name only from it to DirName.
You can use basename command:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=$(basename "$FULLPATH")
You can use built-in bash tricks:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=${FULLPATH##*/}
Explanations:
first # means 'remove the pattern from the begining'
second # means 'remove the longer possible pattern'
*/ is the pattern
Using built-in bash avoid to call an external command (i.e. basename) therefore this optimises you script. However the script is less portable.