Hi,
I wish to change time stamp of a directory with all its subdirectories and files on server.
I am able to find following two ways but want to know which will be the better one. I have not tried anyone of them because I am not sure if it can effect my data:
find * -type d -exec touch '{}/.gitkeep' \;
$ bash recursive_touch.sh MyProject .gitkeep
SOURCE_DIR=$1
FILE=$2
recursive_touch() {
for dir in *
do
if [ -d $dir ]
then
cd $dir; recursive_touch
fi
touch $FILE
done
}
cd $SOURCE_DIR; recursive_touch
Thanks
The "find" version is preferable because it uses less system resources to execute and is IMHO a lot easier to read and maintain.
Note that "touch" will create a file if it isn't already there, therefore your version will not only update existing files ".gitkeep", but also create one in every directory if it ins't already there. If you only want to touch already existing files you will want to change your "find" command:
find * -type f -name ".gitkeep" -exec touch '{}' \;
"touch" will only change the content of the i-node of a file (the modification timestamp), but not change the contents of the file itself.
I hope this helps.
bakunin
Thanks
Note that "touch" will create a file if it isn't already there, therefore your version will not only update existing files ".gitkeep", but also create one in every directory if it ins't already there.
If a file is not there, how its possible to create any random file. I am little confused with it.
So if the name of my directory is Practical and I want to change time stamp of it with all its subdirectories and files, I can use this command.
find * -type f -Practical ".gitkeep" -exec touch '{}' \;
Thanks
"touch" will create an empty file (not a random one!). Try it out. Create and change to a temporary directory, and issue "touch file" there, then issue "ls". You will see a file named "file" with a content of 0 bytes. You can remove this directory along with the file by going one level higher and issue "rm -rf <directory>"
cd /tmp
mkdir tempdir
cd tempdir
touch file
ls -l
cd ..
rm -rf tempdir
No, not quite. Do it this way:
find /some/where/Practical -type f -name ".gitkeep" -exec touch '{}' \; # changes existing ".gitkeep" files
find /some/where/Practical -type d -exec touch '{}' \; # changes existing directories
I hope this helps.
bakunin
Can you please explain regarding gitkeep. 
I don't quite understand: "touch" changes the time stamp of an existing file or directory. If the filename given does not exist it will create the file with the respective time stamp.
The first command searches for all files of the name ".gitkeep" in a directory structure given and uses "touch" on these files. The second one does the same with the directories in this tree.
I hope this helps.
bakunin