Solved

Shell command to list file path, file names, and file size.

Posted on 2012-03-28
3
415 Views
Last Modified: 2012-03-30
I would like a single line command in shell that will produce the following output:

/path/to/file/|3333.txt|374

/path/to/file is the root path to all the text files in sub directories built from this path
3333.txt is the file name of the text file found in the path
374 is the file size in bytes of the file

I want this output so I can dump this into an array and separate it by |
I only need text (txt) files and don't want . or .. or anything else. Just the text files recursively from the /path/to/file/ directory.
0
Comment
Question by:jasonslogan
3 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37777794
What would be the input?  Which directory would you be in ?

If you are in the same directory as the files are, something like this would work

 ls -l  | grep "^-" |awk -v path=$(pwd) -F" " '{print path"|"$NF"|"$4}'

Open in new window

0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 37780256
If you need a recursive version, only reporting *.txt files and allowing for spaces in file names:
find /path/to/file -type f -name "*.txt" | while read f
do
echo ${dirname "$f")'|'$(basename "$f")'|'$(stat -c "%s" "$f")
done > result.dat

Open in new window

It will be a lot slower that farzanj's awk version.
0
 
LVL 1

Author Closing Comment

by:jasonslogan
ID: 37789339
It worked great, thank you!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question