Hi,
Please help in this regard.

Can the modification time of a file be updated to any date of our choice?

Please advise.

Thanks in advance!

Dani AI

Generated

was right to point at touch, and correctly noted that different Unix variants implement touch differently. The behavior you saw (a usage message with only -a -m -c -f) means the touch on that system does not accept a timestamp argument — on that implementation -m just updates mtime to now. That is why your attempts kept setting the file to the current time.

If you need to set an arbitrary timestamp and your touch lacks the flag, two practical alternatives:

# GNU coreutils (if available)
touch -d "2009-11-14 11:30:00" f1.sh
# Portable approach using Python (calls utime)
python - <<'PY'
import os, time
t = int(time.mktime(time.strptime('2009-11-14 11:30:00','%Y-%m-%d %H:%M:%S')))
os.utime('f1.sh', (t,t))
PY

To get only the modification timestamp (or just the hour) in a robust way, prefer stat over parsing ls output (parsing ls can break with different locales, long filenames, or varying column layouts):

# GNU stat
stat -c %y f1.sh

# Extract hour (Linux)
stat -c %y f1.sh | cut -d' ' -f2 | cut -d: -f1

# BSD/macOS style stat
stat -f '%Sm' -t '%Y-%m-%d %H:%M:%S' f1.sh

Troubleshooting checklist: run which touch and man touch to see what flags exist; consider installing GNU coreutils if you want -d/-t; remember filesystem/time resolution (FAT, some network filesystems) can round timestamps; and timezone/NTP differences can make “expected” values look off. For the csh newline question: csh echo does not reliably expand \n; use printf or a here-document to append real newlines.

Recommended Answers

All 14 Replies

Hi,
Please help in this regard.

Can the modification time of a file be updated to any date of our choice?

Please advise.

Thanks in advance!

Look into the command touch

Yes Aia , I checked on 'touch' cmd . but it does not work , i think it wont work in cshell? pl confirm..

Yes it will. Why don't you try showing us what you tried.

P.S. Did you try typing "man touch" first?

Hi All,
I used this, but its updating the modfication time to current time.

touch -m 20091114113000 f1.sh

where.. date is 14th nov 2009 & time is 11:30:00
[[CC]YY]MMDDhhmm[.SS]


where each two digits represents the following:
MM
The month of the year [01-12].
DD
The day of the month [01-31].
hh
The hour of the day [00-23].
mm
The minute of the hour [00-59].
CC
The first two digits of the year (the century).
YY
The second two digits of the year.
SS
The second of the minute [00-61].

My another question is ...

How do we get the modification date of a file.?
I just/only want the modification date.?

i cant use 'ls' cmd, this displays all fields

Please help!

Thanks

touch -m 20091114113000 f1.sh
touch -mt 200911141130.00 f1.sh

Take a closer look at the man page.

My another question is ...

How do we get the modification date of a file.?
I just/only want the modification date.?

i cant use 'ls' cmd, this displays all fields

Please help!

Thanks

The man page for ls and the awk command.

-E           The same as -l, except  displays  time  to  the
                  nanosecond  and  with  one format for all files
                  regardless       of       age:       yyyy-mm-dd
                  hh:mm:ss.nnnnnnnnn (ISO 8601:2000 format).

                  In addition, this option  displays  the  offset
                  from  UTC  in  ISO  8601:2000  standard  format
                  (+hhmm or -hhmm) or no characters if the offset
                  is  indeterminable.  The  offset  reflects  the
                  appropriate standard  or  alternate  offset  in
                  force  at  the  file's displayed date and time,
                  under the current timezone.
ls -E f1.sh | awk '{print $6, $7, $8}'

[...]
I just/only want the modification date.?[...]
i cant use 'ls' cmd, this displays all fields
[...]

As you have been shown you can pipe the ls command to another program that further parses the output, in this case awk.
awk can do a lot of things, but even in its simplest form it can be very helpful dealing with strings. Considerate this:

ls -l | awk '{ print $8 " was modified on " $6 }'

Hi Masijade,
I used the cmd given by you , but its showing this & date isnt changing.

Please help

touch -mt 200911141130.00 f1.sh
usage: touch [-amcf] file ...

Hi All,
I have another question
Does \n not work in csh?
If not, then how do I get to put my text in a new line...

Please help

echo "${var} \n" >> ${MAIL_FILE}

Hi Masijade,
I used the cmd given by you , but its showing this & date isnt changing.

Please help

touch -mt 200911141130.00 f1.sh
usage: touch [-amcf] file ...

I didn't mention, that was on Solaris. On something else the "t" may not be necessary (and is seemingly not on your system), but that decimal point is (when including seconds).

Hi All,
I have another question
Does \n not work in csh?
If not, then how do I get to put my text in a new line...

Please help

echo "${var} \n" >> ${MAIL_FILE}

There is a tutorial at the head of the scripting forum here. Read it.

All,
Thanks a lot.

One more question..
how do I get only the HOURS (no minutes) from the below code?

ls -l f3.sh | awk '{print $7}'

I just know that for getting current date HOURS, this is used

date '+%H'

Please help..........

Thanks in advance

If you want to continue using awk as the workhorse, then:

ls -l f3.sh | awk '{ if( NF > 7 ) { split($7, a, ":"); print a[1] } }'

# if( NF > 7 ) checks that there are more than seven fields
# split($7, a, ":") splits field seven into parts according to delimiter ":" and stores it into array a
# print a[1] displays part 1 which must be the hour

Hi Aia,
Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.