get the file name that has specific extension in shell script - bash

I have three files in a directory that has the structure like this:
file.exe.trace, file.exe.trace.functions and file.exe.trace.netlog
I want to know how can I get file.exe as file name?
In other world I need to get file name that has the .trace extension? I should note that as you can see all the files has the .trace part.

If $FILENAME has the name, the root part can be gotten from ${FILENAME%%.trace*}
for FILENAME in *.trace; do
echo ${FILENAME%%.trace*}
done

You can also use basename:
for f in *.trace; do
basename "$f" ".trace"
done
Update: The previous won't process files with extra extensions besides .trace like .trace.functions, but the following sed will do:
sed -r 's_(.*)\.trace.*_\1_' <(ls -c1)
You can also use it in a for loop instead:
for f in *.trace*; do
sed -r 's_(.*)\.trace.*_\1_' <<< "$f"
done

Try:
for each in *exe*trace* ; do echo $each | awk -F. '{print $1"."$2}' ; done | sort | uniq

Related

Batch change multiple file names in bash and save output

I'm trying to change multiple file names with a for loop.
This works to send the output to the screen:
for i in *.gz; do echo $i | sed 's/\-//g'; done
However, when I try to overwrite the file name using sed -i, I get this error:
for i in *.gz; do echo $i | sed -i 's/\-//g'; done
sed: no input files
Any suggestions?
there is a command for this
$ rename - '' *.gz
NB. this is the standard one, not the advanced perl version.
Use Perl rename instead:
rename 's/-//g' *.gz
Or use simple parameter expansion:
for i in *.gz; do mv -- "$i" "${i//-}"; done

Bash: Listing all file with .addr_book without file extensions

I wish to list all files in current directory with the file extension ".addr_book" without the extension being visible.
I have used ls | sed -e 's/\..*$//' but this displays all files in current directory. Is there a way to have it so it only displays .addr_book files.
Thank you.
You could use basename:
for i in *.addr_book; do
basename "$i" .addr_book
done
From man basename:
basename NAME [SUFFIX]
...
Print NAME with any leading directory components removed. If speciā€
fied, also remove a trailing SUFFIX.
for file in *.addr_book; do
echo "${file%.addr_book}" # removes the trailing '.addr_book' from $file
done
Try:
for f in *.addr_book; do
[ -e "$f" ] && printf "%s\n" "${f%.*}"
done
[ -e "$f" ] is used in case there are no *.addr_book files present, otherwise there would be a false result..
You could do some like this:
ls | awk -F"\.addr_book" 'NF>1 {print $1}'
This will divide all file name using .addr_book as separator.
If number field is larger then 1 print first part.
I would recommend this very easy way I use often:
(Option -C1 formats the ls output to one column only, being this the file name)
ls -C1 *.addr_book | awk -F. '{ print $1 }'
Hope this helps!
Daniel.

bash removing part of a file name

I have the following files in the following format:
$ ls CombinedReports_LLL-*'('*.csv
CombinedReports_LLL-20140211144020(Untitled_1).csv
CombinedReports_LLL-20140211144020(Untitled_11).csv
CombinedReports_LLL-20140211144020(Untitled_110).csv
CombinedReports_LLL-20140211144020(Untitled_111).csv
CombinedReports_LLL-20140211144020(Untitled_12).csv
CombinedReports_LLL-20140211144020(Untitled_13).csv
CombinedReports_LLL-20140211144020(Untitled_14).csv
CombinedReports_LLL-20140211144020(Untitled_15).csv
CombinedReports_LLL-20140211144020(Untitled_16).csv
CombinedReports_LLL-20140211144020(Untitled_17).csv
CombinedReports_LLL-20140211144020(Untitled_18).csv
CombinedReports_LLL-20140211144020(Untitled_19).csv
I would like this part removed:
20140211144020 (this is the timestamp the reports were run so this will vary)
and end up with something like:
CombinedReports_LLL-(Untitled_1).csv
CombinedReports_LLL-(Untitled_11).csv
CombinedReports_LLL-(Untitled_110).csv
CombinedReports_LLL-(Untitled_111).csv
CombinedReports_LLL-(Untitled_12).csv
CombinedReports_LLL-(Untitled_13).csv
CombinedReports_LLL-(Untitled_14).csv
CombinedReports_LLL-(Untitled_15).csv
CombinedReports_LLL-(Untitled_16).csv
CombinedReports_LLL-(Untitled_17).csv
CombinedReports_LLL-(Untitled_18).csv
CombinedReports_LLL-(Untitled_19).csv
I was thinking simply along the lines of the mv command, maybe something like this:
$ ls CombinedReports_LLL-*'('*.csv
but maybe a sed command or other would be better
rename is part of the perl package. It renames files according to perl-style regular expressions. To remove the dates from your file names:
rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv
If rename is not available, sed+shell can be used:
for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done
The above loops over each of your files. For each file, it performs a mv command: mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" where, in this case, sed is able to use the same regular expression as the rename command above. s/[0-9]{14}// tells sed to look for 14 digits in a row and replace them with an empty string.
Without using an other tools like rename or sed and sticking strictly to bash alone:
for f in CombinedReports_LLL-*.csv
do
newName=${f/LLL-*\(/LLL-(}
mv -i "$f" "$newName"
done
for f in CombinedReports_LLL-* ; do
b=${f:0:20}${f:34:500}
mv "$f" "$b"
done
You can try line by line on shell:
f="CombinedReports_LLL-20140211144020(Untitled_11).csv"
b=${f:0:20}${f:34:500}
echo $b
You can use the rename utility for this. It uses syntax much like sed to change filenames. The following example (from the rename man-page) shows how to remove the trailing '.bak' extension from a list of backup files in the local directory:
rename 's/\.bak$//' *.bak
I'm using the advice given in the top response and have put the following line into a shell script:
ls *.nii | xargs rename 's/[f_]{2}//' f_0*.nii
In terminal, this line works perfectly, but in my script it will not execute and reads * as a literal part of the file name.

Renaming files in a UNIX directory - shell scripting

I have been trying to write a script that will take the current working directory, scan every file and check if it is a .txt file. Then take every file (that's a text file), and check to see if it contains an underscore anywhere in its name and if it does to change the underscore to a hyphen.
I know that this is a tall order, but here is the rough code I have so far:
#!/bin/bash
count=1
while((count <= $#))
do
case $count in
"*.txt") sed 's/_/-' $count
esac
((count++))
done
What I was thinking is that this would take the files in the current working directory as the arguments and check every file(represented by $count or the file at "count"). Then for every file, it would check if it ended in .txt and if it did it would change every underscore to a hyphen using sed. I think one of the main problems I am having is that the script is not reading the files from the current working directory. I tried included the directory after the command to run the script, but I think it took each line instead of each file (since there are 4 or so files on every line).
Anyway, any help would be greatly appreciated! Also, I'm sorry that my code is so bad, I am very new to UNIX.
for fname in ./*_*.txt; do
new_fname=$(printf '%s' "$fname" | sed 's,_,-,')
mv "$fname" "$new_fname"
done
why not:
rename 's/_/-/' *.txt
$ ls *.txt | while read -r file; do echo $file |
grep > /dev/null _ && mv $file $(echo $file | tr _ -); done
(untested)
Thanks for all your input guys! All in all, I think the solution I found was the most appropriate for my skill level was:
ls *.txt | while read -r file; do echo file |
mv $file $(echo $file | sed 's,_,-,');
done
This got what I needed done, and for my purposes I am not too worried about the spaces. But thanks for all your wonderful suggestions, you are all very intelligent!

Rename several files in the BASH

I would like to rename files numbering: I have a files with '???' format I need to put them in '????'.
myfile_100_asd_4 to myfile_0100_asd_4
Thanks
Arman.
Not so elegant SOLUTION:
#/bin/bash
snap=`ls -t *_???`
c=26
for k in $snap
do
end=${k}
echo mv $k ${k%_*}_0${k##*_}_asd_4
(( c=c-1 ))
done
This works for me because I have myfile_100 files as well.
Use rename, a small script that comes with perl:
rename 's/(\d{3})/0$1/g' myfile_*
If you pass it the -n parameter before the expression it only prints what renames it would have done, no action is taken. This way you can verify it works ok before you rename your files:
rename -n 's/(\d{3})/0$1/g' myfile_*
just use the shell,
for file in myfile*
do
t=${file#*_}
f=${file%%_*}
number=$(printf "%04d" ${t%%_*})
newfile="${f}_${number}_${t#*_}"
echo mv "$file" "$newfile"
done
There's a UNIX app called ren (manpage) which supports renaming multiple files using search and substitution patterns. You should be able to cobble together a pattern that will inject that extra 0 into the filename.
Edit: Project page w/ download link can be found at Freshmeat.
Try:
for file in `ls my*`
do
a=`echo $file | cut -d_ -f1`
b=`echo $file | cut -d_ -f2`
c=`echo $file | cut -d_ -f3,4`
new=${a}_0${b}_${c}
mv $file $new
done

Resources