Solved

Help for a  unix  command

Posted on 2013-05-10
2
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

729 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