Solved

Help for a  unix  command

Posted on 2013-05-10
2
336 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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now