LinuxQuestions.org
Review your favorite Linux distribution.
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 03-27-2014, 05:44 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
mv with exclusion of FULL PATH ?


This is a simple structure of dirs and files where I want to present my problem:
Code:
/testing/backup/1.txt
/testing/backup/2.txt
/testing/backup/3.txt
/testing/backup/test_file
/testing/tmp
/testing/1.txt
/testing/2.txt
/testing/.hidden_file
I would like to:
1. move (mv) all content of /testing to /testing/tmp
2. exclude /testing/1.txt but /testing/backup/1.txt must be included.

I was trying some tricks with FIND and exclusion like this -type f ! \( -name 1.txt\) but then it excluded both /testing/1.txt but /testing/backup/1.txt. It is also not possible to add to this exclusion full path.
Does anyone have some smart idea how to do that move with full path exclusion ?

Temporary my solution for this issue is rsync:
Code:
rsync /testing/* -a --exclude /test/1.txt /testing/tmp

Last edited by czezz; 03-27-2014 at 07:46 AM.
 
Old 03-28-2014, 01:52 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Won't that get you /testing/tmp/tmp/tmp/tmp ...?
 
Old 03-28-2014, 04:34 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The switches to specify full path are -path and -wholename (less portable). I assume that you want to move the whole directory structure into tmp (except tmp itself) otherwise you will end up with some files overwritten by others with the same name (e.g. 2.txt). In this case you may use the option -maxdepth to limit the search to the first level of the directory tree.

Here is an example:
Code:
$ find /testing -maxdepth 1 ! -path /testing/1.txt
/testing
/testing/.hidden_file
/testing/backup
/testing/tmp
/testing/2.txt
However we still need to exclude the /testing directory itself and the /testing/tmp directory. The latter to avoid the annoying error message from mv that reminds we cannot move a directory to a sub-directory of itself. Here we go:
Code:
$ find /testing -mindepth 1 -maxdepth 1 ! -path /testing/1.txt ! -path /testing/tmp
/testing/.hidden_file
/testing/backup
/testing/2.txt
Hope this helps.
 
1 members found this post helpful.
Old 03-31-2014, 04:57 AM   #4
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Hi, thank you for your answer. That worked perfectly and solved my problem, thanks
That is full command that I used.
Code:
find /testing -mindepth 1 -maxdepth 1 ! -path /testing/1.txt ! -path /testing/tmp -exec mv {} /testing/tmp \
I also tried to play a little bit with this command and I see is it easy to adjust it to different scenarios.
Eg. to skip file: /testing/backup/1.txt
So, I modified command like this:
Code:
find /testing -mindepth 1 -maxdepth 1 ! -path /testing/backup/1.txt ! -path /testing/tmp -exec mv {} /testing/tmp \;
find /testing -mindepth 1 -maxdepth 2 ! -path /testing/backup/1.txt ! -path /testing/tmp -exec mv {} /testing/tmp \;
but it failed...
dir /testing/tmp has not been moved (thats good) but /testing/backup/1.txt has been moved.
Why is that ?.



UPDATE:
and it is odd because by issuing this I have a good output:
Code:
find /testing -mindepth 1 -maxdepth 2 ! -path /testing/backup/1.txt ! -path /testing/tmp
/testing/2.txt
/testing/.hidden_file
/testing/1.txt
/testing/backup
/testing/backup/2.txt
/testing/backup/test_file
/testing/passwd_link
... but it does not work with mv command.


UPDATE #2
I think I know why. Its probably because of this:
Code:
find /testing -mindepth 1 -maxdepth 2 ! -path /testing/backup/1.txt ! -path /testing/tmp
/testing/2.txt
/testing/.hidden_file
/testing/1.txt
/testing/backup
/testing/backup/2.txt
/testing/backup/test_file
/testing/passwd_link
...and if Im right its quite tricky to make it working now

Last edited by czezz; 03-31-2014 at 07:28 AM.
 
Old 03-31-2014, 07:02 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This is because from your output
Code:
/testing/2.txt
/testing/.hidden_file
/testing/1.txt
/testing/backup
/testing/backup/2.txt
/testing/backup/test_file
/testing/passwd_link
the backup directory (highlighted in red) is moved recursively into tmp, and all the files inside it are moved accordingly without exclusion. At this point the command line would become far more complicate and not mnemonic, hence I suggest as alternative the command rsync, for example:
Code:
rsync -av --dry-run --remove-source-files --exclude tmp --exclude backup/1.txt /testing/ /testing/tmp
Some notes:
  • the --dry-run is for testing purposes: rsync will only list the file to copy without actually copy them. If the list is ok for you, run it again without this option.
  • the --remove-source-files deletes the original file after the transfer, so use it if you're absolutely sure of the results from the --dry-run above.
  • The slash at the end of the source path /testing/ is mandatory to copy the content of the /testing directory (hidden files included) and not the directory /testing itself.
Hope this helps.
 
Old 03-31-2014, 09:11 AM   #6
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Professor Colucix, your help is priceless. As always
Now I also better understand your explanation for -maxdepth
Thank you.

ps. I think it should be --remove-sent-files instead of --remove-source-files

Last edited by czezz; 03-31-2014 at 09:26 AM.
 
  


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] Full Path Name Juve IT Linux - Newbie 6 12-16-2010 08:22 PM
Shell: tr can only be used when given full path (despite being in $PATH) chochem Programming 2 06-27-2008 08:37 AM
Full Path Verbal Kint Linux - General 7 01-30-2008 09:34 PM
full path of a file ttilt Linux - General 1 11-08-2005 06:02 PM
get full path of a command (in C) Hady Programming 9 11-21-2003 10:50 AM

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

All times are GMT -5. The time now is 09:07 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