Du command and modified date in ssh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Du command and modified date in ssh
# 1  
Old 12-21-2014
Du command and modified date in ssh

Hello,

I am a new user to linux and having trouble completing this task. I want to list a directory of files and folders including the name, human readable size, and last modified date. So far I can get two of the three but not all together. For example:

Using ls -lh gives me

Code:
drwxr-xr-x  3 newuzer newuzer    29 Aug  7 03:26 who am i now?
drwxr-xr-x  2 newuzer newuzer    72 Apr  9  2014 WWW folder name 1
-rw-r--r--  1 newuzer newuzer  3.3M Aug  1 10:57 Walrus news.epub
-rw-r--r--  1 newuzer newuzer  699M Aug 17 04:52 wonderful whales.avi
drwxr-xr-x  4 newuzer newuzer    47 Aug 18 05:24 Wyynot2
-rw-r--r--  1 newuzer newuzer   70M Aug  7 10:27 zylopedia.pdf
drwxr-xr-x  3 newuzer newuzer    21 Jul  7 02:19 zzz folder5

This is showing me the name, date, and file size for files only, the directories are not showing the size properly

Using du -hs * gives me

Code:
1.7G   who am i now?
18M    WWW folder name 1
3.3M   Walrus news.epub
699M  wonderful whales.avi
28G    Wyynot2
70M    zylopedia.pdf
3.1M   zzz folder5

The directories are now showing the correct size, the files still have the correct size , but the date/time is missing. I dont really need the permissions so that missing is ok.

How can i get all three name, date/time modified, and size to display? I would like the entries listed as they appear in the directory -files then folders- or with folders/files list in alphabetical order

Any assistance provided would be appreciated.

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for sample commands. You can use ICODE tags for short in-line text in the middle of a sentence.

Last edited by Don Cragun; 12-21-2014 at 03:56 AM.. Reason: Change existing CODE tags to ICODE tags; add missing CODE tags.
# 2  
Old 12-21-2014
The ls output is showing the size of the files listed (note that a directory is just one type of a file).

The du -hs output is showing the sizes of the blocks allocated to the file and (when the file is a directory) the size of the blocks allocated to the directory added to the sizes of the blocks allocated to all files in the file hierarchy rooted in that directory. Note also that the sizes of files in a file hierarchy reported by du -hs might not be what you expect. If there are hard links to files in a hierarchy, the blocks allocated to each linked file will only be counted once. Therefore, the sum of the sizes reported by:
Code:
du -hs dir1 dir2

might not be the sum of the sizes reported by:
Code:
du -hs dir1;du -hs dir2

And, if there are sparse files, the file size may be huge, but the number of blocks allocated to the file may be tiny.

And note that the size of a directory varies from filesystem type to filesystem type. On some filesystem types, the size of a directory shown by ls will be the size of the directory entries in that directory; in other filesystem types, it will just be the number of directory entries contained in that directory; and in other filesystem types, it might be something else. (On some filesystem types, some directory entries can be stored in the i-node for the directory. This makes the size of the directory zero until enough space is needed by directory entries to overflow the space in the i-node.)
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-21-2014
Code:
paste -d' ' \
<(du -hs * | awk -F'\t' '{printf "%8s\n", $1}') \
<(ls -lh | awk 'NR>1{for (i=6;i<=NF;i++) printf "%s ", $i; print ""}')

The paste command lets the two input streams merge.

<(du -hs * ...)
Only first column (correct dir/file size) is used and printed right aligned.
<(ls -lh ...)
All columns from (including) column 6 until the end of the line are used (timestamp + file name).
Note I used NR>1 in the awk command to skip the first line, because the first line produced by ls -lh is total 56K (on my system, in the test directory) which is not needed. You may want to remove NR>1 if there is no total line in your output produced by ls -lh.
This User Gave Thanks to junior-helper For This Post:
# 4  
Old 12-21-2014
Thank you Don for the background and Junior Helper for your code. Here's what happened: first i put the
Code:
paste -d' ' \

in and pressed enter which took me to the next line, then i put in
Code:
(du -hs * | awk -F'\t' '{printf "%8s\n", $1}') \

which gave me an error msg
Code:
-bash: syntax error near unexpected token `('

, i then put in the next line
Code:
(ls  -lh | awk 'NR>1{for (i=6;i<=NF;i++) printf "%s ", $i; print  ""}')

and it returned the date/time and name columns
Code:
Aug 7 03:26 who am i now?
Apr  9  2014 WWW folder name 1
Aug  1 10:57 Walrus news.epub
Aug 17 04:52 wonderful whales.avi
Aug 18 05:24 Wyynot2
Aug  7 10:27 zylopedia.pdf
Jul  7 02:19 zzz folder5

So, it partially worked Im just missing the file size column

---------- Post updated at 10:42 AM ---------- Previous update was at 10:33 AM ----------

Also, if i wanted to remove the
Code:
awk

command, is it just that or that plus some of the other characters?
# 5  
Old 12-21-2014
You forgot the < signs. They are mandatory.

This is the same command in one line:
Code:
paste -d' ' <(du -hs * | awk -F'\t' '{printf "%8s\n", $1}') <(ls -lh | awk 'NR>1{for (i=6;i<=NF;i++) printf "%s ", $i; print ""}')

---------- Post updated at 06:03 PM ---------- Previous update was at 05:57 PM ----------

Regarding removal of the awk command:
Code:
<(ls -lh | awk 'NR>1{for (i=6;i<=NF;i++) printf "%s ", $i; print ""}')

All the code marked light-blue is awk.
If you remove the awk code, then the vertical bar (pipe) is not needed, simply use <(ls -lh).

---------- Post updated at 06:16 PM ---------- Previous update was at 06:03 PM ----------

Code:
paste -d' ' <(du -hs *) <(ls -lh)

might work too, but the result will be accordingly.
# 6  
Old 12-21-2014
Thanks Junior-Helper this is almost perfect. The only issue is the justification. This information is going to be put into excel and im trying to avoid additional manipulation/trouble with the import if the columns arent aligned.
Right now the output is size column right justified - which is perfect, but the date/time and name columns are there but not justified properly. Im looking for it to have the justification as if the
Code:
ls -lh

command was run. Again, this is because its going into excel and there will be a few thousand entries to correct.

Also, can you direct me to a good source to learn more about how the syntax for different commands should go?

---------- Post updated at 11:40 AM ---------- Previous update was at 11:40 AM ----------

sorry, i didnt see your 6:16pm post before i added mine. I will test the new line in a few minutes and see if its working for me. thanks

---------- Post updated at 11:49 AM ---------- Previous update was at 11:40 AM ----------

just tried
Code:
paste -d' ' <(du -hs *) <(ls -lh)

didnt come out too good. The best way is your previous method. I just need help on the justification part now for the date/time and name columns. - thanks

---------- Post updated at 12:04 PM ---------- Previous update was at 11:49 AM ----------

Junior Helper,
I will post the results from the last two tests in a minute, your code is wkg but off one line. just a heads up so you dont reply before i add the info

---------- Post updated at 12:23 PM ---------- Previous update was at 12:04 PM ----------

This is where i am:

Using code
Code:
paste -d' ' \
 <(du -hs * | awk -F'\t' '{printf "%8s\n", $1}') \
 <(ls -lh | awk 'NR>1{for (i=6;i<=NF;i++) printf "%s ", $i; print ""}')

the output comes out with all the necessary columns except that date/time and name columns are not justified as in
Code:
ls -lh

using code
Code:
paste -d' ' \
 <(du -hs * | awk -F'\t' '{printf "%8s\n", $1}') \
 <(ls -lh)

the output comes out with all required colums - size, date/time, name justified correctly, except that the size column starts where the Total folder size line begins
Code:
<ls -lh)

resulting in all lines of the output being off one (showing the previous lines actual size).
What should be modified to get this corrected?

Thanks very much

Last edited by Newuzer; 12-21-2014 at 01:36 PM..
# 7  
Old 12-21-2014
Quote:
What should be modified to get this corrected?
Not sure if I understand correctly, but try
Code:
<(ls -lh | sed 1d)

The sed command will remove the first line of the output produced by ls -lh

---------- Post updated at 09:38 PM ---------- Previous update was at 08:09 PM ----------

Quote:
Originally Posted by Newuzer
The best way is your previous method. I just need help on the justification part now for the date/time and name columns. - thanks
Try this:
Code:
paste -d' ' \
 <(du -hs * | awk -F'\t' '{printf "%8s\n", $1}') \
 <(ls -lh | awk 'NR>1{for (i=6;i<=NF;i++)
    if ( ((i==7) && (length($i)==1)) || ((i==8) && (length($i)==4)) ) printf " %s ", $i; else printf "%s ", $i;
    print ""}')

The long if line means following: if the field 7 (day of month) is a single digit (field length = 1) OR the field 8 (time) is a year (field length = 4), "space-pad" the particular value (first print the space, then the value + a trailing space), else print it "as is" + add a trailing space.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Arrange file by modified date

Hi, Am performing a find based on filename and result can contain multiple files being found Let's say my find command is find /Archive -f -name 12345.pdf and result of find command is /Archive/Folder A/12345.pdf /Archive/Folder B/12345.pdf please note white space in folder names I... (2 Replies)
Discussion started by: gigagigosu
2 Replies

2. AIX

How do I display a file's last modified date?

I'm using a script that I need to get a file's "last modified date" in a format like 01:51:14 PM. We are running on AIX 6.1.0.0. I can't seem to find the right command parameters. Help! (4 Replies)
Discussion started by: mattadams1983
4 Replies

3. Shell Programming and Scripting

Script to copy creation date over top of modified date?

Can someone draw up a script that for every file, folder and subfolder and files that will copy the creation date over top of the modified date?? I know how to touch every file recursively, but no idea how to read a files creation date then use that to touch the modification date of that file,... (3 Replies)
Discussion started by: toysareforboys
3 Replies

4. Shell Programming and Scripting

current date modified file

Hi , In my directory , i have many days file but i want to see all those which are of todays date. i tried this but it gives all the files mtime -0 |ls -ltr I tried the below option as well. 19635 find -iname "*.LOG" -mtime 19636 ls -ltr *.LOG -mtime -1 19637 ls -ltr *.LOG... (7 Replies)
Discussion started by: guddu_12
7 Replies

5. UNIX for Dummies Questions & Answers

Find last modified date for many files

Hello all - I've looked and have not been able to find a "find" command that will list the last modified date of files within a specific directory and its subdirectories. If anyone knows of such a command it would be very much appreciated! If possible, I would like to sort this output and have... (5 Replies)
Discussion started by: MichaelH3947
5 Replies

6. Shell Programming and Scripting

search by modified date

Hello, How can i search for all the files in a particular directory which are not updated from past 5 days. Thanks, Sateesh (4 Replies)
Discussion started by: kotasateesh
4 Replies

7. UNIX for Dummies Questions & Answers

Last Modified Date for subfolders

Sorry for the basic question, but I have a feeling that my developers are circumventing our change control process, and I want to be able to easily keep track of the last modified date of sub-folders of the production folder. Basically, we have this major folder PROD, and then each application... (1 Reply)
Discussion started by: saint01
1 Replies

8. Shell Programming and Scripting

greping certain modified date

i have a k shell script that grep less than certain modified date ------------------------------------------------- #!/bin/ksh for i in * do day=`ls -ltr | grep $i | tr -s " " | cut -d " " -f6-7 | awk 'substr($0,7,4)substr($0,1,5)substr($0,11)<"Feb 1"' ` echo $day done... (2 Replies)
Discussion started by: ali560045
2 Replies

9. Shell Programming and Scripting

Last Modified Date

Hello, I'm on a practical training and i have to change a shell script which print out a HTML-File with all Printers (and features example: IP, Mac etc.) they have in the factory. The Features of the Printers are on each separate file. i mean every printer have an own file with it features. ... (12 Replies)
Discussion started by: cengiz
12 Replies

10. UNIX for Dummies Questions & Answers

How do I get the last modified date of a file?

I am trying to load a group of files and their last dates modified into a text file that will in turn be used with SQL*Loader to load these files into Oracle. I am using a *.ksh script. I am getting the name of the file in by using the following: for file_ext in 'cat loaddir.ext'; do find... (2 Replies)
Discussion started by: akpopa
2 Replies
Login or Register to Ask a Question