0

so I have a line of code like this:

result=`find . -type f -size -1000c -print0 | xargs -0 ls -Sh | head`

for i in $result; do
    item=`wc -c $i`
    echo $item1 
done

this will print out all the files in the current fold that are at most 1000bytes, it has the format like:

size_of the file ./name_of_the_file

but i want to get rid of the "./" symbol, so i try to use "cut"

i want to do something like:

for i in $result; do
    item=`wc -c $i`
    item1=`cut -f 1 $item`    // this gives me the size 
    item2=`cut -c 7- $item`   // this gives me all the character after ./ 
    echo item1, item2         // now make it print 
done

but i'm getting error like:

cut: 639: No such file or directory

can anyone please give me a hint on this? I appreciate it.

2
  • Never store file lists in a variable, and never ever parse ls. And use $() instead of backticks to capture the output. Also, you are not giving cut a file or pipe to process. You are giving it an argument $item which is just a string, and apparently not a valid filename. You would need either echo "$item" | cut -f 1 or cut -f 1 <<<"$item". However, cut is also tricky about the delimiters. See answers below for a proper solution.
    – orion
    Jan 22, 2015 at 9:25
  • Also, wouldn't you need +1000c instead of minus to get files larger than the limit?
    – orion
    Jan 22, 2015 at 9:29

4 Answers 4

2

Depending on your system's support for POSIX, I believe this will give you the same result you are trying to achieve:

    find . -type f -size -1000c -printf '%P %s Bytes\n'
0

Look at the Synopsis of cut(1):

cut OPTION... [FILE]...

It's expecting its last argument to be a file (or list of files). What you're doing is supplying $item as the last argument. It seems that $item is 639 which would explain the error message: you're passing 639 in a place where cut is expecting a file name argument. You need to replace $item in those cut calls with $i.

I think the best way to extract the file name (minus ./) would be to use parameter expansion instead of relying on a fixed number of characters to be there every time:

item2="${i#*./}"
0

try this:

ls -l | awk '$5 <= 1000{print $5, $9}'
2
  • What happens when the filenames have spaces, or newlines in them?
    – Anthon
    Jan 22, 2015 at 7:00
  • @Anthon , very good question, this line can't handle the case you mentioned, although spaces and newlines are not recommend to be part of file names, they still should be taken good care of.
    – comeonfox
    Jan 22, 2015 at 9:24
0
find . -type f -size -1000c -printf "%s %f\n"

%s prints File's size in bytes.
%f prints File's name with any leading directories removed (only the last element).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .