Solved

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

Posted on 2012-03-28
3
408 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sendmail STARTTLS error 37 77
Linux  Script File Attributes 5 51
Backup & Restore 3 46
su - oracle could not open session 6 47
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now