[shell scripting] Small problem, for each file in directory...
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
[shell scripting] Small problem, for each file in directory...
Hello, yes i am working on some homework, however i am not here to be spoon fed.
I am trying to get the numerical modification date of each file in a folder.
Ie lets say there is a file called bob and it was modified 2006-11-23
i want to get it into a variable as 20061123
now i currently have this code:
Code:
#!/bin/sh
# Backup Function
backup()
{
echo "Backing up files"
for filename in *
do
# convert file date to compare date
modifieddate=`ls --full-time $filename |awk '{print $6}'`
year=`echo $modifieddate | cut -d '-' -f1`
month=`echo $modifieddate | cut -d '-' -f2`
day=`echo $modifieddate | cut -d '-' -f3`
compare="$myear$mmonth$mday"
echo $mcomparable
done
return
}
backup
ls, l, lc, lf, lr, lx -- list contents of directories
Description
===========
-l
Lists in long format, giving mode, number of links, owner, group,
size in bytes, the time that each file was last modified. The -1
option is assumed. For files less than six months old, minutes and
seconds are included in the modification time and the year is
excluded. For a consistent format, use -T.
-T
Similar to -l, except that the modification time is always displayed
in full, as in this example:
Aug 31 09:37:00 2001
Unless one of the -c or -u options is specified, the modification
time refers only to changes made to the file's data, or the
creation of the file. It does not record the time that changes were
made to the information stored in the inode.
If $filename contained spaces or file name pattern matching characters such as * ? [ ] it could expand to more than one file name, so two or more lines would be piped to awk.
To stop that happening try putting $filename in double quotes in this line
will pick-up the year of the second date. You have to investigate why the $filename variable contains two file names at some point. In any case, you can avoid the cut command and use the substring extraction in bash, example:
will pick-up the year of the second date. You have to investigate why the $filename variable contains two file names at some point. In any case, you can avoid the cut command and use the substring extraction in bash, example:
Note that the first character in a string is character number 0 for the shell, so that to extract the 9th and 10th characters the start index is 8.
Hmm thank you for the suggestion, i just can't understand why this is happening, i believe using the substring extraction is beyond what we have learnt.
Is it a problem with one of the files themselves, or with my script? I have tried changing around a few things but to no avail .
The reason is that some of the listed files are directories! If the argument of the ls command is a directory, the output is a list of the content of the directory, not just the directory itself. To avoid this problem use the -d option of ls:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.