LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2021, 09:50 PM   #1
1s440
Member
 
Registered: Mar 2018
Posts: 266

Rep: Reputation: Disabled
script not working


Hi all,

I wanted to move recursively all the folders present under the path /temp/source/ to /temp/destionation/tobemoved/ with respect to the specific date.

When i try with find command unfortunately it doesnot work

for f in `find /temp/source/ -name "*.*"`; do mv $f /temp/destionation/tobemoved/; done

can anyone help me with this

Last edited by 1s440; 02-17-2021 at 11:49 PM.
 
Old 02-18-2021, 01:24 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
I would first try that find command itself if it reports exactly what you need.
Code:
find /temp/source/ -type d
will print all the subfolders. -name is not required.
 
Old 02-18-2021, 01:25 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,353
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
It will help if you include [code] [/code] tags around your script.

What about an approach like this?

Code:
#!/bin/sh

find /temp/source/ \
        -type d \
        -name "*.*" \
        -exec mv -t /temp/destination/tobemoved/ {} \;
See -exec in "man find" and of course "man mv" too.

Note that with that -name option, the folder names must have a dot in them or they will not be found.

Last edited by Turbocapitalist; 02-18-2021 at 01:26 AM.
 
Old 02-18-2021, 04:06 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
I would suggest looking up rsync unless you wish to practice shell scripting instead
 
Old 02-18-2021, 04:45 AM   #5
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I would suggest looking up rsync unless you wish to practice shell scripting instead
I was looking with rsync —remove-source files but it dies not actually delete the source directories.
 
Old 02-18-2021, 07:35 AM   #6
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
It will help if you include [code] [/code] tags around your script.

What about an approach like this?

Code:
#!/bin/sh

find /temp/source/ \
        -type d \
        -name "*.*" \
        -exec mv -t /temp/destination/tobemoved/ {} \;
See -exec in "man find" and of course "man mv" too.

Note that with that -name option, the folder names must have a dot in them or they will not be found.
I wanted to move the directories under "source" to the destination folder "tobemoved" . here its moving the directory "source".

Last edited by 1s440; 02-18-2021 at 08:05 AM.
 
Old 02-18-2021, 07:48 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by 1s440 View Post
somehow this find command not working
Quote:
Originally Posted by Turbocapitalist View Post
Note that with that -name option, the folder names must have a dot in them or they will not be found.
Then omit the -name "*.*" \ part.

Last edited by shruggy; 02-18-2021 at 07:50 AM.
 
Old 02-18-2021, 08:56 AM   #8
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Then omit the -name "*.*" \ part.
if i wanted to move files older than one day then

Code:
#!/bin/sh

find /etkon/mounts/nc/Archiv/* \
        -type d \
        -mtime+1 -exec mv -t /etkon/mounts/archive/ {} \;

Last edited by 1s440; 02-18-2021 at 08:59 AM.
 
Old 02-18-2021, 08:58 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,353
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
Quote:
Originally Posted by 1s440 View Post
I wanted to move the directories under "source" to the destination folder "tobemoved" . here its moving the directory "source".
Throw in a -mindepth then.

Code:
find /temp/source/ \
        -mindepth 1 \
        -type d \
        -name "*.*" \
        -exec mv -t /temp/destination/tobemoved/ {} \;
 
Old 02-18-2021, 09:25 AM   #10
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Throw in a -mindepth then.

Code:
find /temp/source/ \
        -mindepth 1 \
        -type d \
        -name "*.*" \
        -exec mv -t /temp/destination/tobemoved/ {} \;
Thank you. I wonder -mtime +1 doesnot work. if i want to move the folder from the day before. move files older than one day. for ex: i want to move the folders till yesterdays but not today
 
Old 02-18-2021, 09:56 PM   #11
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by 1s440 View Post
Thank you. I wonder -mtime +1 doesnot work. if i want to move the folder from the day before. move files older than one day. for ex: i want to move the folders till yesterdays but not today
For some reason with mtime it’s not working and it’s not moving the directories also. Can you please suggest
 
Old 02-19-2021, 12:37 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Try -mtime +0
Code:
find /temp/source/ \
        -mindepth 1 \
        -maxdepth 1 \
        -type d \
        -mtime +0 \
        -exec mv -t /temp/destination/tobemoved/ {} \;
The -mtime examines the directory - not its files.
Moving a file in or out changes the directory's mtime. Modifying a file changes the file's mtime - not the directory's mtime.
 
Old 02-19-2021, 12:47 AM   #13
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
Try -mtime +0
Code:
find /temp/source/ \
        -mindepth 1 \
        -maxdepth 1 \
        -type d \
        -mtime +0 \
        -exec mv -t /temp/destination/tobemoved/ {} \;
The -mtime examines the directory - not its files.
Moving a file in or out changes the directory's mtime. Modifying a file changes the file's mtime - not the directory's mtime.

Thanks for the reply. if I use -mtime in the find command it doesnot move directories itself .
Code:
drwxrwxrwx 36 nasa nasa 4096 Feb 19 06:54 16022021
drwxrwxrwx 47 nasa nasa 4096 Feb 19 06:59 17022021
drwxrwxrwx 10 nasa nasa 4096 Feb 19 07:39 18022021
drwxrwxrwx 22 nasa nasa 4096 Feb 19 07:39 15022021
 
Old 02-19-2021, 02:47 AM   #14
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
The -type d restricts the find targets to directories.

Do not have -name "*.*" that would require a dot in the name!

The -maxdepth 1 restricts to directories directly under the start directory e.g. /temp/source/16022021 matches but not /temp/source/subdir/16022021
 
Old 02-19-2021, 03:25 AM   #15
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
The -type d restricts the find targets to directories.

Do not have -name "*.*" that would require a dot in the name!

The -maxdepth 1 restricts to directories directly under the start directory e.g. /temp/source/16022021 matches but not /temp/source/subdir/16022021
Code:
find /temp/source/ -mindepth 1 -mtime +1
displays me the required directories

Last edited by 1s440; 02-19-2021 at 07:03 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script String Manipulation - not working in script. BW-userx Programming 14 05-22-2017 01:31 AM
[SOLVED] Script not working in cron but working fine manually jasperux Linux - Newbie 2 07-04-2012 11:28 PM
Crontab is not working, the script is working arfal SUSE / openSUSE 6 02-08-2010 08:48 PM
Ethernet Adapter not working or Network not working... phoenix07 Linux - Hardware 2 04-09-2004 06:58 PM
cannot download files in mozilla -save dialog not working, galeon not working cmisip Linux - General 0 08-03-2003 03:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

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