Bash rename string that starts with digits - bash

I have folders with multiple files with names like 2024_CULT_IMAGE_2012_03.shp and CULT_IMAGE_2017_03.shp How can I test if the string begins with digits. I know how to rename them when I have tested them. I have used reg-ex to test if a file contains digits but I am unsure how to test beginning of the string.
if [[ $file =~ [0-9] ]];
then
do something
The output I expect is CULT_IMAGE.shp.

I would use rename utility
rename 's/^\d+_//' *.shp

To remove EVERY digits with sed you can just do sed 's/[0-9]//g'
So you should be able to adapt it quickly to remove only the first digits

Before
ls -lrt
total 4
-rw-rw-r-- 1 user super 173 May 20 09:58 main
-rw-rw-r-- 1 user super 0 May 20 10:13 CULT_IMAGE_2017_1111132.shp
-rw-rw-r-- 1 user super 0 May 20 10:13 2024_CULT_IMAGE_2012_03.shp
#Execution:Rename files which starts with numbers.
./main
After Execution
ls -lrt
total 4
-rw-rw-r-- 1 user super 173 May 20 09:58 main
-rw-rw-r-- 1 user super 0 May 20 10:13 CULT_IMAGE_2017_1111132.shp
-rw-rw-r-- 1 user super 0 May 20 10:13 CULT_IMAGE.shp
File
cat main
for file in *.shp
do
if [[ "$file" =~ "^[0-9]" ]];then
newName=$(echo $file | sed -e 's/[0-9]//g' -e 's/^_//' -e 's/__.shp/.shp/')
mv $file $newName
fi
done

Related

Rename files with consecutive numbers, keeping the original filename

I got a bunch of mp3 files with random names and numbers like:
01_fileabc.mp3
01.filecdc.mp3
fileabc.mp3
929-audio.mp3
For sorting purposes, I need to add a sequential number in front of the file name like:
001_01_fileabc.mp3
002_01.filecdc.mp3
003_fileabc.mp3
004_929-audio.mp3
I checked some of the solutions I found here. One of the first solutions worked kind of but replaced the filename instead of adding to it.
num=0; for i in *; do mv "$i" "$(printf '%04d' $num).${i#*.}"; ((num++)); done
How can I modify this command to add to the filename instead?
I am sorry, but whatever I try I can't find a solution myself here.
Just replace ${i#*.} (which stands for "Remove from $i from the left up to the first dot) with $i, which is the original name of the file (I'd probably use $filename, $oldfile, or at least $f instead of $i as the variable's name).
You can also replace the . before it with _, otherwise the files will be named
0001.01_fileabc.mp3
etc.
UPDATE: As RobC commented about this answer, existing whitespace or newline characters can cause problems listing files because of using ls command with bash arrays. So the above code can be improved in this way
#!/bin/bash
i=0
for file in *.mp3; do
i=$((i+1))
mv "$file" "$(printf "%03d_%s" "$i" "$file")"
done
ORIGINAL ANSWER: You can try this code in a bash script. Remember to make it executable with
$ chmod +x script.sh.
#!/bin/bash
contents_dir=($(ls *.mp3))
for file in ${!contents_dir[*]}; do
new=$(awk -v i="$file" -v cd="${contents_dir[$file]}" 'BEGIN {printf("%03d_%s", i+1, cd)}')
mv ${contents_dir[$file]} $new
done
It will add a consecutive 0-leaded tree digits number as you wanted to all mp3 files found in the dir where the script is executed.
You could try this …
$ ls -l
total 4
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 01.filecdc.mp3
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 01_fileabc.mp3
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 929-audio.mp3
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 fileabc.mp3
$ t=0
$ for i in *mp3
> do
> # Use the seq command to get a formatted zero filled string.
> prefix=$(seq -f "%04g" $t $t)
>
> # Move $i to new file name.
> mv $i ${prefix}_${i}
>
> # Increment our counter, t.
> t=$(expr $t + 1)
> done
$ ls -l
total 4
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 0000_01.filecdc.mp3
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 0001_01_fileabc.mp3
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 0002_929-audio.mp3
-rw-r--r-- 1 plankton None 5 Nov 4 13:35 0003_fileabc.mp3

how to move specific files based on a key and rename them

I have over 100000 files.
for example, I mentioned 3 files below
bcbb79d8-1d4a-4fbb-b16c-4df86839773e.htseq.counts.gz
bcdc68db-c874-4097-9c46-b06e331caaf5.htseq.counts.gz
bd4b6975-90d9-43f8-aadc-344d04644822.htseq.counts.gz
I have a text file named key.txt with the following information.
File Name ID
bcbb79d8-1d4a-4fbb-b16c-4df86839773e.htseq.counts.gz TCCC-06-0210
bcdc68db-c874-4097-9c46-b06e331caaf5.htseq.counts.gz TCHA-27-2519
bd4b6975-90d9-43f8-aadc-344d04644822.htseq.counts.gz TCHU-76-4929
I want to take only those files that their name are in the key , move them to a new folder and change their name to the ID.
I guess a little more of a write up rather than a comment would be helpful. The approach to take is to read the filename (fname) and ID (id) from each line in key.txt and then validate that fname is a file and does exist, and then move the file in "$fname" to whatever "/path/to/move/to/$id" you need.
For example:
#!/bin/bash
## read each line into variables fname and id (handle non-POSIX eof)
while read -r fname id || [ -n "$fname" ]; do
## test that "$fname" is a file, and if so, move to destination
[ -f "$fname" ] && mv "$fname" "/path/to/move/to/$id"
done < key.txt
(note: a POSIX end-of-file (eof) is simply the final '\n' at the end of the last line. Some editors do not enforce it and it will cause your read to miss the final line of data unless you check that "$fname" was filled with data (is non-empty) -- the [ -n "$fname" ] added to the end of the white read -r ...)
You are feeding the loop with a redirection of key.txt. Each iteration of the while loop will read a new line from key.txt into the variables fname and id (word-splitting on the default Internal Field Separator (IFS). After the read and separation into fname and id, you simply verify $fname holds a valid filename (in the current working directory) and then mv the file where you want it.
You should execute the script in the directory containing the files, or append a relative or absolute filename to where they are located to "$fname".
Example
Here is a short example that may help clear things up:
The move_rename.sh script:
$ cat move_rename.sh
#!/bin/bash
## read each line into variables fname and id (handle non-POSIX eof)
while read -r fname id || [ -n "$fname" ]; do
## test that "$fname" is a file, and if so, move to destination
[ -f "$fname" ] && mv "$fname" "dest/$id.txt"
done < key.txt
The key.txt file:
$ cat key.txt
File Name ID
bcbb79d8-1d4a-4fbb-b16c-4df86839773e.htseq.counts.gz TCCC-06-0210
bcdc68db-c874-4097-9c46-b06e331caaf5.htseq.counts.gz TCHA-27-2519
bd4b6975-90d9-43f8-aadc-344d04644822.htseq.counts.gz TCHU-76-4929
File locations before script execution. (dest) is the directory to move to. (that is ls -one output not ls -L(lowercase), the ls -al is `L(lowercase))
$ ls -1
dest
bcbb79d8-1d4a-4fbb-b16c-4df86839773e.htseq.counts.gz
bcdc68db-c874-4097-9c46-b06e331caaf5.htseq.counts.gz
bd4b6975-90d9-43f8-aadc-344d04644822.htseq.counts.gz
key.txt
move_rename.sh
$ ls -al dest
total 16
drwxr-xr-x 2 david david 4096 Jan 17 20:05 .
drwxr-xr-x 16 david david 12288 Jan 17 20:05 ..
Execute the script
$ bash move_rename.sh
Working directory contents after execution
$ ls -1
dest
key.txt
move_rename.sh
Contents of dest after execution.
$ ls -al dest
total 8
drwxr-xr-x 2 david david 4096 Jan 17 20:00 .
drwxr-xr-x 3 david david 4096 Jan 17 20:00 ..
-rw-r--r-- 1 david david 0 Jan 17 19:59 TCCC-06-0210.txt
-rw-r--r-- 1 david david 0 Jan 17 19:59 TCHA-27-2519.txt
-rw-r--r-- 1 david david 0 Jan 17 19:59 TCHU-76-4929.txt

How to add formatting to, and batch rename filenames?

I have around 7,000 .txt files that have been spat out by a program where the naming convention clearly broke. The only saving grace is that they follow the following structure: id, date, time.
m031060209104704.txt --> id:m031 date:060209 time:104704.txt
Sample of other filenames (again same thing):
115-060202105710.txt --> id:115- date:060202 time: 105710.txt
x138051203125338.txt etc...
9756060201194530.txt etc..
I want to rename all 7,000 files in this directory to look like the following:
m031060209104704.txt --> 090206_104704_m031.txt
i.e date_time_id (each separated by underscores or hyphens, I don't mind). I need the date format to be switched from yymmdd to ddmmyy as shown directly above though!
I'm not clear on whats overkill here, full program script or bash command (MAC OS). Again, I don't mind, any and all help is appreciated.
Try something like:
#!/bin/bash
# directory to store renamed files
newdir="./renamed"
mkdir -p $newdir
for file in *.txt; do
if [[ $file =~ ^(....)([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{6})\.txt$ ]]; then
# extract parameters
id=${BASH_REMATCH[1]}
yy=${BASH_REMATCH[2]}
mm=${BASH_REMATCH[3]}
dd=${BASH_REMATCH[4]}
time=${BASH_REMATCH[5]}
# then rearrange them to new name
newname=${dd}${mm}${yy}_${time}_${id}.txt
# move to new directory
mv "$file" "$newdir/$newname"
fi
done
Bash string indexes makes it very easy and efficient to rework the filenames as you intend. You should also validate you are only operating on input filenames of 20 characters. That can be accomplished as follows:
#!/bin/bash
for i in *.txt; do
## validate a 20 character filename
(( ${#i} == 20 )) || { printf "invalid length '%s'\n" "$i"; continue; }
echo "mv $i ${i:8:2}${i:6:2}${i:4:2}_${i:10:6}_${i:0:4}.txt" ## output rename
mv "$i" "${i:8:2}${i:6:2}${i:4:2}_${i:10:6}_${i:0:4}.txt" ## actual rename
done
Example Directory
$ ls -l
total 0
-rw-r--r-- 1 david david 0 Dec 21 19:16 115-060202105710.txt
-rw-r--r-- 1 david david 0 Dec 21 19:16 9756060201194530.txt
-rw-r--r-- 1 david david 0 Dec 21 19:15 m031060209104704.txt
-rw-r--r-- 1 david david 0 Dec 21 19:16 x138051203125338.txt
Example Use/Output
$ cd thedir
$ bash ../script.sh
mv 115-060202105710.txt 020206_105710_115-.txt
mv 9756060201194530.txt 010206_194530_9756.txt
mv m031060209104704.txt 090206_104704_m031.txt
mv x138051203125338.txt 031205_125338_x138.txt
$ ls -l
total 0
-rw-r--r-- 1 david david 0 Dec 21 19:42 010206_194530_9756.txt
-rw-r--r-- 1 david david 0 Dec 21 19:42 020206_105710_115-.txt
-rw-r--r-- 1 david david 0 Dec 21 19:42 031205_125338_x138.txt
-rw-r--r-- 1 david david 0 Dec 21 19:42 090206_104704_m031.txt
Look things over and let me know if you have any further questions.

Batch rename files in sequence

I am having trouble renaming image sequences in the shell.
I have about 300 files following the pattern myimage_001.jpg and I would like to transform that to myimage.0001.jpg where the numbers increment with each file.
This is what I have tried with no success (the -n flag being there to show the result before actually applying it):
rename -n 's/_/./g' *.jpg
Try this command :
rename _ . *.jpg
Example :
> touch myimage_001.jpg
-rw-r--r-- 1 oracle oinstall 0 Mar 17 10:55 myimage_001.jpg
> rename _ . *.jpg
> ll
-rw-r--r-- 1 oracle oinstall 0 Mar 17 10:55 myimage.001.jpg
With an extra 0 :
> touch myimage_001.jpg
-rw-r--r-- 1 oracle oinstall 0 Mar 17 10:55 myimage_001.jpg
> rename _ .0 *.jpg
> ll
-rw-r--r-- 1 oracle oinstall 0 Mar 17 10:55 myimage.0001.jpg
the syntax is simple :
rename 'old' 'new' 'data-source'
Works fine for me? Note however that this does not add an additional leading zero like in your question, was this a typo?
$ find
.
./myimage_001.jpg
./myimage_007.jpg
./myimage_006.jpg
./myimage_002.jpg
./myimage_004.jpg
./myimage_009.jpg
./myimage_008.jpg
./myimage_003.jpg
./myimage_005.jpg
$ rename -n 's/_/./g' *.jpg
myimage_001.jpg renamed as myimage.001.jpg
myimage_002.jpg renamed as myimage.002.jpg
myimage_003.jpg renamed as myimage.003.jpg
myimage_004.jpg renamed as myimage.004.jpg
myimage_005.jpg renamed as myimage.005.jpg
myimage_006.jpg renamed as myimage.006.jpg
myimage_007.jpg renamed as myimage.007.jpg
myimage_008.jpg renamed as myimage.008.jpg
myimage_009.jpg renamed as myimage.009.jpg
$ rename 's/_/./g' *.jpg
$ find
.
./myimage.008.jpg
./myimage.007.jpg
./myimage.001.jpg
./myimage.003.jpg
./myimage.006.jpg
./myimage.005.jpg
./myimage.002.jpg
./myimage.009.jpg
./myimage.004.jpg
You can try something like:
for file in *.jpg; do
name="${file%_*}"
num="${file#*_}"
num="${num%.*}"
ext="${file#*.}"
mv "$file" "$(printf "%s.%04d.%s" $name $num $ext)"
done
This gives:
$ ls
myimage_001.jpg myimage_002.jpg
$ for file in *.jpg; do
name="${file%_*}"
num="${file#*_}"
num="${num%.*}"
ext="${file#*.}"
mv "$file" "$(printf "%s.%04d.%s" $name $num $ext)"
done
$ ls
myimage.0001.jpg myimage.0002.jpg
Another alternative:
$ touch a.txt b.txt c.txt d.txt e.txt f.txt
$ ls
a.txt b.txt c.txt d.txt e.txt f.txt
We can use ls combined with sed + xargs to achieve your goal.
$ ls | sed -e "p;s/\.txt$/\.sql/"|xargs -n2 mv
$ ls
a.sql b.sql c.sql d.sql e.sql f.sql
See http://nixtip.wordpress.com/2010/10/20/using-xargs-to-rename-multiple-files/ for detailed information.

How to decrement a number in each filename in a directory?

Running "ls -lrt" on my terminal I get a large list that looks something like this:
-rw-r--r-- 1 pratik staff 1849089 Jun 23 12:24 cam13-vid.webm
-rw-r--r-- 1 pratik staff 1850653 Jun 23 12:24 cam12-vid.webm
-rw-r--r-- 1 pratik staff 1839110 Jun 23 12:24 cam11-vid.webm
-rw-r--r-- 1 pratik staff 1848520 Jun 23 12:24 cam10-vid.webm
-rw-r--r-- 1 pratik staff 1839122 Jun 23 12:24 cam1-vid.webm
I have only shown part of it above as a sample.
I would like to rename all the files to have a number one less than current.
For example,
mv cam1-vid.webm cam0-vid.webm
mv cam2-vid.webm cam1-vid.webm
.....
....
mv cam 200-vid.webm cam199-vid.webm
How can this be done using a os x / linux bash script (perhaps using sed) ?
You can do this with plain bash:
for i in {1..200}
do
mv "cam${i}-vid.webm" "cam$((i-1))-vid.webm"
done
I would use find, split up the file names, to find the number, subtract one, and rename:
find . -name "cam*-vid.webm" -print0 | while read -d\$0 old_name
do
number=${old_name#cam} #Filter left to remove 'cam' prefix
number=${number%-vid.webm"} #Filter right to remove '-vid.webm' suffix
$((number -= 1))
new_name="cam${number}-vid.webm"
echo "mv \"$old_name\" \"$new_name\""
done | tee results
This will merely print out the commands (that is why I have echo). I'm piping it into a file named results. Once this command completes, look at results and make sure it does everything it should. Whenever there's an operation like this, there can be a nasty surprise. For example, if I rename cam02-vid.webm to cam01-vid.webm before I rename cam01-vid.webm, I am going to overwrite cam01-vid-webm.
Maybe a safer way is to explicitly give the file numbers I need:
for number in {1..200}
do
$((old_number = $number + 1))
echo mv "\"cam${old_number}-vid.webm\" \"cam${number}-vid.webm\""
done | tee results
Useful hint: If the result file looks good, you can actually just run it as a shell script:
$ bash results
Another possibility is to test to make sure the old file exist:
for number in {1..200}
do
$((old_number = $number + 1))
if [ -f "$cam${old_number}-vid.webm" ]
then
echo mv "\"cam${old_number}-vid.webm\" \"cam${number}-vid.webm\""
else
echo "ERROR: Can't find a file called 'cam${old_number}-vid.webm'"
fi
done | tee results
A perl solution.
First it traverses all input files (#ARGV) and filters those that are plain files and not links (grep), extracts the number (map) and sorts numerically in ascendant to avoid overwritting (sort). Later creates a new file decrementing the number and renames the original:
perl -e '
for (
sort { $a->[0] <=> $b->[0] }
map { m/(\d+)/; [$1, $_ ] }
grep { -f $_ && ! -l $_ }
#ARGV
) {
$n = --$_-> [0];
($newname = $_->[1]) =~ s/\A(?i)(cam)\d+(.*)\z/$1$n$2/;
print "Executing command ===> rename $_->[1], $newname\n";
rename $_->[1], $newname;
}' *
Assuming initial content of the directory as:
cam1-vid.webm
cam13-vid.webm
cam12-vid.webm
cam11-vid.webm
cam10-vid.webm
cam2-vid.webm
After running the command yields:
cam0-vid.webm
cam10-vid.webm
cam11-vid.webm
cam12-vid.webm
cam1-vid.webm
cam9-vid.webm

Resources