07-24-2010
Doing this portably is more difficult than it sounds.. though anything is possible.
The "find" command excels at finding files that are newer than a target time. So, if the goal is finding files within a tree that are newer than a specified time, that can be done with "find". If this isn't Linux or a platform with GNU find, then the -newer argument of find can be used with a file argument where that file's mtime is used as the target time mentioned above. You can use the touch command to create the file with the timestamp for which you want "find" to find files newer than.
I realize that tons of people are using Linux now or have a good "find" (e.g. GNU find), but I like to show people the "portable" way.
Consider:
touch -t 201007230000 reftime.stamp
(you'll have to do a "man" on "touch" to see if your accepts a -t option or expects the time string, possibly in a different format, simply as an argument)
Then you could do:
find /dir1/dir2/dir3old -type f -newer /fullpathto/reftime.stamp
To get a list of the files that are newer than the time stamp.
Now it would be easy to do a copy at this point using something like cpio to copy the files over... but that isn't what you asked for. You want them to move.. and I don't blame you... just an inode remove/add to a directory entry (very fast).
So you want something like:
mv /dir/dir2/dir3old/p1/p2/p3/file1 /dir1/dir2/dir3new/p1/p2/p3/file1
As long as the destination file1 doesn't exist, which you make it sound like it does not, and the directories already exist on the destination side, then we can filter our list of files into the corresponding "mv" commands to feed to the shell.
Note: the following makes an assumption that the filenames do not contain special characters, but spaces are allowed for.
(this is a one liner)
find /dir1/dir2/dir3old -type f -newer /fullpathto/reftime.stamp |
sed 's,^/dir1/dir2/dir3old/\(.*\),mv "/dir1/dir2/dir3old/\1" "/dir1/dir2/dir3new/\1",'
The nice thing about using a filter like the above is we can run and and safely see the output and see if it makes sense before piping that to a shell.
As I said, it makes assumptions based on what you said... not sure if it's true though. Also, sometimes it helps to know EXACTLY what you are wanting to do this for and on what OS... then it's VERY likely that a more elegant and correct solution can be presented.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I have a requirement to move a file from one folder(a) to another folder(b) only when folder (b) have a write permission. Folder permission is 755
If the permission is otherthan 755 we need to come out of the loop
I will appreciate your help
Thanks
Soll (1 Reply)
Discussion started by: sollins
1 Replies
2. UNIX for Dummies Questions & Answers
Hi!
I don't use to program much with unix but it would be useful to create a script to move a folder to a default directory.
For example I right click on a directory and the script moves it to the home folder
At the moment I've this but doesn't work:
and I also tryed this
and... (0 Replies)
Discussion started by: ruben.rodrigues
0 Replies
3. Shell Programming and Scripting
This is the file structure:
DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg
DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg
DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg
DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg
DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies
4. Shell Programming and Scripting
Hi, I have a requirement to move to diff directories based on the input file
Project|SourceFolder|SourceFile|TargetFolder|TargetFile
the above is my Record in a file I have to read the record and move to the Target folder to copy some records
var_TargetSystem==`cat "$var_SourceFile" |... (3 Replies)
Discussion started by: magesh_bala
3 Replies
5. Shell Programming and Scripting
I want to move all the files inside a directory to another directory which is inside that directory. Please help.
Note: I want to move only the files. Note the folders. Thanks in advance. (4 Replies)
Discussion started by: brnl_basu
4 Replies
6. Shell Programming and Scripting
Hi all,
let me explain my requirments
i am having 5 folder with different name for eg) abc , cdf , efd, rtg, ead
each 5 folders contain 15 files
i want to move 10 files to some other folder, remain 5 files should be there in the same folder.
give me some suggestion on this. (6 Replies)
Discussion started by: natraj005
6 Replies
7. Shell Programming and Scripting
Hi All,
I have to requirement to write a shell script to move file from one folder (A) to another five folder (B,C,D,E,F) and destination folder should be blank. In not blank just skip.
This script will run as a scheduler every 2 minutes. It will check number of files in folder A and move 1 to... (9 Replies)
Discussion started by: peekuabc
9 Replies
8. Shell Programming and Scripting
I want to move a folder with spaces from one folder to another.
I have two folders like this,
1).RT_032-222 -4444-01/ 2). RT_032-555 -7777-01/
I want to move files from 2 to 1 through shell script.Here I want to assign this like a user defined variable like as Source branch... (2 Replies)
Discussion started by: kannansoft1985
2 Replies
9. UNIX for Beginners Questions & Answers
what is the best way to move the contents of a folder to another one without deleting the structure of the first one. the contents could include subfolder too.
both folder, the source-folder and the target-folder are on the same host.
any idea is appreciated . (7 Replies)
Discussion started by: andy2000
7 Replies
10. Shell Programming and Scripting
Hi Team,
I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder.
Eg:
parent folder --> /Interface/data/test/IN
Sub folder -->/Interface/data/test/IN/Invoice20180607233338
Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies
dircmp(1) User Commands dircmp(1)
NAME
dircmp - directory comparison
SYNOPSIS
dircmp [-ds] [-w n] dir1 dir2
DESCRIPTION
The dircmp command examines dir1 and dir2 and generates various tabulated information about the contents of the directories. Listings of
files that are unique to each directory are generated for all the options. If no option is entered, a list is output indicating whether the
file names common to both directories have the same contents.
OPTIONS
The following options are supported:
-d Compares the contents of files with the same name in both directories and output a list telling what must be changed in the two
files to bring them into agreement. The list format is described in diff(1).
-s Suppresses messages about identical files.
-w n Changes the width of the output line to n characters. The default width is 72.
OPERANDS
The following operands are supported:
dir1 A path name of a directory to be compared.
dir2
USAGE
See largefile(5) for the description of the behavior of dircmp when encountering files greater than or equal to 2 Gbyte ( 2**31bytes).
ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of dircmp: LC_COLLATE, LC_CTYPE, LC_MES-
SAGES, and NLSPATH.
EXIT STATUS
The following exit values are returned:
0 Successful completion.
>0 An error occurred. (Differences in directory contents are not considered errors.)
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|Availability |SUNWesu |
+-----------------------------+-----------------------------+
SEE ALSO
cmp(1), diff(1), attributes(5), environ(5), largefile(5)
SunOS 5.10 1 Feb 1995 dircmp(1)