Batch rename files if they don't contain a character - console

I have audio files names either 'A_B.wav' or 'A.wav'. I'd like to rename all files with a filename in the format 'A.wav' to 'A_A.wav' and leave all other files unchanged.
In other words, I need to batch rename only files that contain no underscore.
Is there a way to do this via the Linux console?

#!/bin/bash
for i in *;
do
j=`echo $i | cut -d . -f 1`;
e=`echo $i | cut -d . -f 2`;
if [[ $j != *_* ]]
then
j=$j"_"$j"."$e;
mv $i $j;
fi
done

Related

paste text to filenames in unix

I have a list of strings in String that I want to add at the beginning of all files names of Targets in the folder. All files are ordered.
String.txt:
ID1Somestring_
IDISomeOtherString_
IDISomeThirdString_
Targets:
example1.fastq
example2.fastq
example3.fastq
output:
ID1Somestring_example1.fastq
IDISomeOtherString_example2.fastq
IDISomeThirdString_example3.fastq
First, read the file into an array
mapfile -t strings < String.txt
Then, iterate over the files and access each array element in turn:
n=0; for file in *fastq; do echo mv "$file" "${strings[n++]}$file"; done
mv example1.fastq ID1Somestring_example1.fastq
mv example2.fastq IDISomeOtherString_example2.fastq
mv example3.fastq IDISomeThirdString_example3.fastq
Or, assuming your filenames do not contain newlines
paste String.txt <(printf "%s\n" *fastq) |
while read -r string file; do echo mv "$file" "$string$file"; done

unix command to redirects output to a file

I am trying to write a unix command which will write/redirects the output to a file i.e. create a file if there is difference in 2 files else it will not create the file.
I am using the below command but it always creates a file(of 0B if no diff), no matter there is any difference in file or not.
diff -u -w a.txt b.txt > diff.tmp
I am trying to write a single unix command that will create file "diff.tmp" if "a.txt" is not equal to "b.txt" else "diff.tmp" will not be created.
Thanks in advance,
Pritish
In bash you could remove it afterwards:
diff -u -w a.txt b.txt > diff.tmp && if [ -f diff.tmp ] && [ ! -s diff.tmp ]; then rm diff.tmp; fi
Note:
-f: to check if the file exits (-e to check if a file, directory, etc. exists)
-s: to check if the file is non-zero
However can will work for text files ..you can use cmp command as well.
cmp a.txt b.txt > cmp.tmp && if [ -f cmp.tmp ] && [ ! -s cmp.tmp ]; then rm cmp.tmp; fi
you can check return code of diff. From man page:
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.
So I would write something like:
#!/bin/bash
diff "$1" "$2" 2>/dev/null 1>/dev/null
if [[ $? -eq 0 ]];then
echo "No diff found!"
else
echo "Diff saved in file "$3
diff $1 $2 > $3
fi
And then you call it like
./diff.sh a.txt b.txt diff.tmp
Hope it helps!
Bye
Piero

Not getting expected file result using awk

#!/bin/bash
delete_file () {
for file in processor_list.txt currnet_username.txt unique_username.txt
do
if [ -e $file ] ;then
rm $file
fi
done
}
delete_file
ps -elf > processor_list.txt ; chmod 755 processor_list.txt
awk '{print $3}' processor_list.txt > currnet_username.txt ; chmod 755 currnet_username.txt
sort -u currnet_username.txt > unique_username.txt ;chmod 755 unique_username.txt
while read line ; do
if [ -e $line.txt ] ;then
rm $line.txt
fi
grep $line processor_list.txt >$line.sh ;chmod 755 $line.sh
awk '{if($4 == "$line") print $0;}' $line.sh > ${line}1.txt ; #mv ${line}1.txt $line.txt;chmod 755 $line.txt
done < unique_username.txt
I'm a beginner of unix shell scripting. please suggested, i am not getting expected results in ${line}1.txt.
For example, I have two UID like kplus , kplustp. what is my requirement is find "kplus" string from ps -elf command and create a file as same name like kplus.txt and redirect or move the data whatever found data using grep command.
But I am getting kplus and kplustp data in kplus.txt file. I need only kplus value based on UID column from ps –elf in kplus.txt file.
This is wrong way to read variable using awk
awk '{if($4 == "$line") print $0;}' $line.sh
Use:
awk '{if($4 == var) print $0;}' var="$line" $line.sh
Or shorten to
awk '$4==var' var="$line" $line.sh
default action is {print $0} if no action is specified.
If you need to search for the text $line escape the $ in regex
awk '$4==/\$line/' $line.sh
or in text it should work directly
awk '$4=="$line"' $line.sh

KSH sort filenames

I'm searching through a number of directories for "searchstring", and then running a script on each $file:
for file in `find $dir -name ${searchstring}'*'`;
do
echo $file >> $debug
script.sh $file >> $output
done
My $debug file yields the following:
/root/0007_searchstring/out/filename_20120105_020000.log
/root/0006_searchstring/out/filename_20120105_010000.log
/root/0005_searchstring/out/filename_20120105_013000.log
(filename is _yyyymmdd_hhmmss.log)
...
Is there a way to get find to order by filename or by mktime? Should I pipe find to sort first? Make an array then sort it as per this question?
If you want to ignore the directory path and just use the file name, then you should be able to use:
for file in `find $dir -name ${searchstring}'*' | sort --field-separator=/ --key=4`;
'ls -t' if you need to regenerate the list based on timestamp.
'sort -n' if the list is fairly static?
To sort by modification time, you can use stat with find:
$ find . -exec stat {} -c '%Y %n' \; | sort -n | cut -d ' ' -f 2
You can pipe the output of find through sort to sort by filename:
find $dir -name "${searchstring}*" | sort | while read file
do
echo "$file" >> $debug
script.sh "$file" >> $output
done

Unix script to delete file if it contains single line

Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it contains single line.
Something like, if 'wc -l abscde.txt' = 1 then rm abscde.txt
My system : Solaris
Here's a simple bash script:
#!/bin/bash
LINECOUNT=`wc -l abscde.txt | cut -f1 -d' '`
if [[ $LINECOUNT == 1 ]]; then
rm -f abscde.txt
fi
delifsingleline () {
if [ $(cat $1 | wc -l) = "1" ]
then
echo "Deleting $1"
echo "rm $1"
fi
}
Lightly tested on zsh. Should work on bash as well.
This is (mostly) just a reformat of Ben's answer:
wc -l $PATH | grep '^1 ' > /dev/null && rm -f $PATH

Resources