0

e.g. I have directory like ABC. And this directory contain following files (it contain number of files i just include 4 fils here)

2014-04-29T06-19-20_17368f93ce.mp4.uploaded
2014-04-29T06-19-20_17368f93ce.mp4
2014-04-29T06-18-20_17368f93ce.mp4

I have one file_name variable it contain one of the file name that present in theABC directory.

i.e. file_name=2014-04-29T06-19-20_17368f93ce.mp4

I am using the following command to find total count of files that ends with only .mp4 in *ABC* directory

ls -1 /home/ABC/*.mp4 | wc -l

So i just want to ignore file in the count that are in the file_name variable

Means file_count just ignore the file that are in file_name.

  • Is this a bash script that you are creating? if so, could you paste in the code. Is the file_name variable an array? – Just Lucky Really Apr 29 '14 at 9:07
  • @user3139907 - Actually I am using shell command in ruby script this is not shell script. this is ruby script, and file_name is the ruby variable – Prakash V Holkar Apr 29 '14 at 9:32
0

First of all, never parse the output of ls. Here are some safer approaches:

  1. Use find and grep (wc -l fails on filenames that contain newlines):

    find /home/terdon/foo -not -name "$file_name"  -name '*.mp4' | grep -c /
    

    The -not flag makes find ignore any files called $file_name.

  2. In this particular case, where your files don't contain whitespace and you probably don't need to deal with portability and language issues, you can use ls. It is just a bad habit to get into. So, to use your original approach, just filter out the names you don't want using grep -v:

     ls -1 /home/ABC/*.mp4 | grep -v "$file_name" | wc -l
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.