user6456810 user6456810 - 1 month ago 12
Linux Question

How do I use the pipe command to display attributes in a file?

I'm currently making a shell program and I want to display the total amount of bytes in a specific file using the pipe command. I know that the pipe command takes whatever is on the left side and gives it to the right as input. (Assuming you are in the directory the file is in)

I know that the command (

wc -c
) displays the number of bytes in a file but I'm not sure how to pipe it. What I've tried was:

ls | wc -c


wc takes the filename as argument, not as input. Try this:

wc -c

The wc program takes multiple arguments. You can do this to apply it to all entries in the current working directory:

wc -c $(ls)

Another approach is to use xargs to convert input to arguments:

ls | xargs wc -c

You may need to use a more complex line if you have spaces in your filenames. ls can output a single file per line, and xargs can be told to split only on \n:

ls -1 | xargs -d '\n' wc -c

If you prefer to use find instead of ls (a more powerful tool), the -print0 option for find plays along with the -0 option to xargs.