Solved

Help for a  unix  command

Posted on 2013-05-10
2
338 Views
Last Modified: 2013-05-11
Hi,

I am using the below command from a shell script to find file date.


Filedate=`ls -l /usr/home/test/sample.txt | awk '{ print $6 $7}'` ;

echo $Filedate


But it is printing  for ex:

May9   instead of May09

Can anyone please  let me know how to fix this..
0
Comment
Question by:new_perl_user
2 Comments
 
LVL 26

Accepted Solution

by:
pony10us earned 500 total points
ID: 39155895
Taking a wild guess since I don't have access to my Unix box here to test:

Filedate=`ls -l /usr/home/test/sample.txt | awk '{ print $6 ("%02d\n", $7)}'` ; 
echo $Filedate

Open in new window


the ("%02d\n", $7) should create 2 digit with leading zero's for the date I believe.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39157762
My awk (on Ubuntu) threw a syntax error with this.  A more usual way to do this would be:
Filedate=`ls -l /usr/home/test/sample.txt | awk '{ printf "%s%02d\n", $6, $7}'`

Open in new window

By the way, the above still doesn't work on Ubuntu because the date in an "ls -l" is given as YYYY-MM-DD rather than a separate Month and Day.  This also confuses people who assume that the filename is $9 in "ls -l" - in Ubuntu it starts at $8. For example
$ ls -l /etc/hosts
-rw-r--r-- 1 root root 1939 2013-02-15 09:56 /etc/hosts

Open in new window

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 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