how to get the filename from the mentioned list

83 Views Asked by At

I am new to linux and/or scripting, so bear with me. I want a script which can get the files for a Linux directory. here what I tried for getting the filename.

for NAME in $(ls -1 *.wav /some/path | cut -d "/" -f3 | cut -d "-" -f1-5)

if the filename contains -IN or -OUT then they will be sox -m and after that mv to another directory but if the some other files then it will be just mv

for the reference, filenames be like

1030-04-06-2015-1433414216.wav
1030-04-06-2015-1433414318.wav
1030-04-06-2015-1433414440.wav
1043-21-05-2015-1432207256.wav
1043-21-05-2015-1432207457.wav
1046-20-05-2015-1432137944.wav
1046-20-05-2015-1432138015.wav
1046-20-05-2015-1432138704.wav
1431709157.93900.0-in.wav
1431709157.93900.0-out.wav
1431709157.93900.1-in.wav
1431709157.93900.1-out.wav
1431710008.94059.0-in.wav
1431710008.94059.0-out.wav
1431710008.94059.1-in.wav
1431710008.94059.1-out.wav
1431710008.94059.1.wav
1431710008.94059.2.wav
1431713190.94698.2-in.wav
1431713190.94698.2-out.wav
1431713190.94698.2.wav
1431721107.96010.0-in.wav
1431721107.96010.0-out.wav
1431721107.96010.1.wav

2

There are 2 best solutions below

2
On BEST ANSWER

This should work:

#!/bin/bash
regex='(.*)(-out|-in)(.txt)'
for file in *.txt;do
# $file is the full path to the file
  if [[ $file =~ $regex ]];then
  #filename in this block contains -in or -out 
    filename="${file##*\/}"
    inorout="${BASH_REMATCH[2]}"
    extension="${BASH_REMATCH[3]}"
    filenamewithoutextension="${filename%.*}"
    filenamewithoutioroutorextension="${filenamewithoutextension/%$inorout/}"
    filenamewithoutiorout="${filenamewithoutioroutorextension}$extension"
    echo "$filename" "$inorout" "$extension" "$filenamewithoutextension" "$filenamewithoutiorout" "$filenamewithoutioroutorextension"
    #do something here sox-m mv or whatever
  else
  #filename in this block doesn't contain -in or -out
    echo "do something else"
  fi
done


Explanation:

"${file##*\/}" is the string left by cutting */ from $file i.e the basename (filename).

"${BASH_REMATCH[2]}" is the second captured group from the pattern matching done by [[ $file =~ $regex ]] i.e -in or -out

"${BASH_REMATCH[3]}" is the third captured group i.e .wav.

"${filename%.*}" is the string left by cutting .* from $file i.e filename without .wav


Resources you should check on:

  1. Bash Parameter Expansion
  2. Pattern Matching
  3. Bash Variables
1
On

Here is one way to do it.

>cat test.sh
#!/bin/bash

destination="./DEST"

# Loop over every file
for file in "${@}" ; do
  # If it is "-in", then sox
  if [[ "${file}" =~ "-in" ]] || [[ "${file}" =~ "-out" ]] ; then
    printf "sox -m ${file}; "
  fi
  echo "mv ${file} ${destination}"
done

When run, I get the following output.

>../test.sh *
mv 1030-04-06-2015-1433414216.wav ./DEST
mv 1030-04-06-2015-1433414318.wav ./DEST
mv 1030-04-06-2015-1433414440.wav ./DEST
mv 1043-21-05-2015-1432207256.wav ./DEST
mv 1043-21-05-2015-1432207457.wav ./DEST
mv 1046-20-05-2015-1432137944.wav ./DEST
mv 1046-20-05-2015-1432138015.wav ./DEST
mv 1046-20-05-2015-1432138704.wav ./DEST
sox -m 1431709157.93900.0-in.wav; mv 1431709157.93900.0-in.wav ./DEST
sox -m 1431709157.93900.0-out.wav; mv 1431709157.93900.0-out.wav ./DEST
sox -m 1431709157.93900.1-in.wav; mv 1431709157.93900.1-in.wav ./DEST
sox -m 1431709157.93900.1-out.wav; mv 1431709157.93900.1-out.wav ./DEST
sox -m 1431710008.94059.0-in.wav; mv 1431710008.94059.0-in.wav ./DEST
sox -m 1431710008.94059.0-out.wav; mv 1431710008.94059.0-out.wav ./DEST
sox -m 1431710008.94059.1-in.wav; mv 1431710008.94059.1-in.wav ./DEST
sox -m 1431710008.94059.1-out.wav; mv 1431710008.94059.1-out.wav ./DEST
mv 1431710008.94059.1.wav ./DEST
mv 1431710008.94059.2.wav ./DEST
sox -m 1431713190.94698.2-in.wav; mv 1431713190.94698.2-in.wav ./DEST
sox -m 1431713190.94698.2-out.wav; mv 1431713190.94698.2-out.wav ./DEST
mv 1431713190.94698.2.wav ./DEST
sox -m 1431721107.96010.0-in.wav; mv 1431721107.96010.0-in.wav ./DEST
sox -m 1431721107.96010.0-out.wav; mv 1431721107.96010.0-out.wav ./DEST
mv 1431721107.96010.1.wav ./DEST
mv DEST ./DEST

If you want to execute the commands, simply cut and paste these lines into a shell, or modify the bash script to execute these commands rather than print them.