recursive timestamp update in unix - unix-timestamp

When I create a directory say d1 and after 5 seconds d1/d2, then d1 timestamp gets updated to that of d2. After 5 seconds, when I create d1/d2/d3, only d2 timestamp gets updated to d3 but not that of d1.
Basically, my requirement is that not only the parent folder but all the folders from root to parent folder must get updated with the time of the parent folder.
Is there any way to update the timestamp of d1 with that of d3?
Please clarify.

find . -type d -exec touch -m -r d3 {}\;
Finds all directories in the current directory and updates the timestamp to the current time...

find . -type d -exec touch -r d1/d2/d3 -m {} \;
touch options:
-r :reference file. The timestamp of this ref file will be used for touching.
-m :change the modification time.
This will find all the directories under pwd, and will modify the modification time of each to the modification time of d1/d2/d3 directory. It is assumed that you are in the directory that has directory d1.

This will set the modification time of only the directories that are on the path to the added file.
So in this tree
d1
d1/d2
d1/d2/d3 <-- this is the one we are adding
d1/d2a
d1/d2a/d3a
Only d1 and d1/d2 will be affected.
CHILD="d1/d2/d3"
DIR=`dirname "$CHILD"`
while [[ "$DIR" != "." ]]
do
touch -m -r "$CHILD" "$DIR"
DIR=`dirname "$DIR"`
done

Related

Copy directories and their sub directories created day ago

I tried to copy Copy directories and their subdirectories created a day ago as follows:
find /application/work/ -type d -mtime -1 -exec cp -r {} /tmp/backup \;
But it is copying all directories (Not only the ones created a day ago).
Would you please advise?
find is also finding the working directory /application/work/ and is copying it, see How to exclude this / current / dot folder from find "type d". Since you're executing cp -r, it recursively copies everything in . before also copying the subset of directories you've found via -mtime. You need to set the -mindepth to exclude the working directory from the paths on which find will operate.
Modify your command to:
find /application/work -mindepth 1 -type d -mtime -1 -exec cp -r {} /tmp/backup \;

Move files and copy subtree

source="/somedir/dir-a"
dest="/somedir2/dir-z"
I need to find all files recursively within the $source directory which contain the string 720p and move them to $dest
Just 2 things to take care of -
For all such files which are to be moved , first create that file's outer 2 directories in $dest and then move this matched file inside that
i have to do this for lakhs of files so a bit of parallelization would be helpful
Example
For a file like - "$source/dir-b/dir-c/file-720p.mp4" , it should do as follows :
mkdir -p "$dest/dir-b/dir-c"
mv "$source/dir-b/dir-c/file-720p.mp4" "$dest/dir-b/dir-c/file-720p.mp4"
You're looking for something like this:
src=foo
dst=bar
export dst
find "${src}" -name '*720p*' -type f -exec sh -c '
for p; do
np=${dst}${p#"${p%/*/*/*}"}
echo mkdir -p "${np%/*}" &&
echo mv "$p" "$np"
done' sh {} +
This can be parallelized using GNU find's -print0 primary in conjunction with GNU xargs, but I don't think that'd make much of a difference performance-wise, as this is rather an IO-intensive task.
Remove echos if the output is satisfactory.

Copying the files and SUBDIRECTORIES based on modification date?

It may be a duplicate question but i could not find the solution for this i want to copy a last 3 months files AND subdirectories from one disk to andother but i could find only to listing the files by using the following command. I really don't know how to copy the files by using -mtime. I'm new to linux please help me.
find . -mtime -90 -exec cp {} targetdir \;
but how to copy directories with subdirectories and files too? (but do not use command rsync, i don;t have it with this instance) Regards S.
Copy needs a recursive option specified to handle the subdirectories
$ find testroot # shows some dirs and files
testroot
testroot/sub1
testroot/sub1/subtestfile
testroot/sub2
testroot/testf
$ find target # empty at this stage
target
$ find ./testroot/ -exec cp -R {} target/ \;
$ find target
target
target/sub1
target/sub1/subtestfile
target/sub2
target/subtestfile
target/testf

Two ways of entering a directory by inode number?

If I want to enter a directory by its inode number, why
cd $(find . -inum $inode_num)
works, but the following command does not work:
find . -inum $inode_num -exec cd {} \;
what's the difference between these two, and why is the 2nd one wrong?
cd is not a program that can be executed, it's a built-in shell command. It has to be, since it's too hard to change current directory in parent process.

how to prevent "find" from dive deeper than current directory

I have many directory with lots of files inside them.
I've just compressed that directory respectively become filename.tar.gz, someothername.tar.gz, etc.
After compressing, I use this bash to delete everything except file name contains .tar.gz:
find . ! -name '*.tar.gz*' | xargs rm -r
But the problem is find will dive too deep inside the directory. Because the directory has been deleted but find will dive deep in each directory, many messages displayed, such as:
rm: cannot remove `./dirname/index.html': No such file or directory
So how to prevent find from dive deeper than this level (current directory)?
You can use ls instead of find for your problem:
ls | grep -v .tar.gz | xargs rm -rf
You can tell find the max depth to recurse:
find -maxdepth 1 ....