LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-04-2013, 09:28 PM   #1
psycroptic
Member
 
Registered: Aug 2011
Location: USA
Distribution: ArchLinux - 3.0 kernel
Posts: 349

Rep: Reputation: Disabled
how can i remove files from a dir but NOT change the dirs modified date?


title says it all. cp has a "--preserve" option, rm doesn't. Is it even possible? cp --preserve to /dev/null maybe?

Last edited by psycroptic; 08-04-2013 at 09:33 PM.
 
Old 08-04-2013, 09:43 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,

after calling rm you could use touch to reset the modified date back to what is was. Eg something like
Code:
ts=$(stat -c %y somedir)  # Get the time dir was last modified
rm somedir/somefile       # Remove some file... this will alter the dir mod time 
touch --date="$ts" -m somedir  # Reset the dir mod time back to what it was
HTH,

Evo2.
 
Old 08-04-2013, 09:45 PM   #3
psycroptic
Member
 
Registered: Aug 2011
Location: USA
Distribution: ArchLinux - 3.0 kernel
Posts: 349

Original Poster
Rep: Reputation: Disabled
right, i figured it would be a kludge like that, just reading the date before the rm and rewriting it afterwards. i've got a bunch of differently-named files that i need to delete, so it'll be somewhat of a PITA. i would have thought that mv and rm would also include preservation options... guess not.

thanks amigo.

Last edited by psycroptic; 08-04-2013 at 09:46 PM.
 
Old 08-04-2013, 09:59 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Hi,

you can wrap those three lines in a little script. Eg

Code:
#!/bin/bash
set -e
f=$1
d=$(dirname $(readlink -f $f))
ts=$(stat -c %y $d)
\rm $f
touch --date="$ts" -m $d
couple of things to note:
- This script is UNTESTED. Use at own risk (any script containing commands like rm should be treated with suspicion)
- The "set -e" will cause the script to exit on any error.
- \rm is used instead of rm in an attempt to protect against aliases for rm.
- The readlink call is used to get the full path to the file, so that you will get the full path to the directory... careful when using symbolic links.

If you are not familiar with any of the commands used in this script, please carefully read their man pages.

Anyway, HTH,

Evo2.
 
Old 08-04-2013, 10:04 PM   #5
psycroptic
Member
 
Registered: Aug 2011
Location: USA
Distribution: ArchLinux - 3.0 kernel
Posts: 349

Original Poster
Rep: Reputation: Disabled
word, thanks. i was actually prolly going to just handmake a file that contains, line by line, each path to each file, then making a separate script that reads file and removes each using the commands you initially gave. sandwiching those commands within a single "for" statement, i believe it to be possible in 5 lines. 1 less than yours
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to find root files and dirs in some dir? Mr. Alex Linux - Newbie 2 11-01-2010 12:56 AM
How to change modified date? You_You Linux - Newbie 2 08-26-2009 02:25 AM
change modified date stamp of file B-Boy Linux - Newbie 2 08-21-2008 07:33 AM
remove 'modified date' column in nautilus list view nexx_au Linux - Software 0 12-09-2003 03:29 AM
Cmd to rm dir thats has files and other dirs psyklops Linux - General 4 04-17-2002 08:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration