LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-28-2013, 06:17 AM   #1
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Rep: Reputation: Disabled
Question How to copy each sub-directory with another name


I have a parent directory, suppose A, which contains several other directories which in turn contain more directories. So the situation is a 3 level structure
where Parent =A
containing several B1, B2, B3, B4
Each B has directory verilog

Now for every verilog directory i want to
Code:
cp -rpf verilog systemVerilog
.
Plz help
 
Old 01-28-2013, 06:33 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can try find with -execdir to execute the cp command inside the directory where the object (verilog) has been found:
Code:
find /path/to/A -type d -name verilog -execdir cp -rpf '{}' systemVerilog \;
Please, see man find for details.
 
Old 01-28-2013, 10:25 AM   #3
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks colucix, this is the command that should do the work but i'm getting this message when try to execute it from the concerned directory(A)
Quote:
find . -type d -name verilog -execdir cp -rpf '{}' systemVerilog \;
find: The current directory is included in the PATH environment variable, which is insecure in combination with the -execdir action of find. Please remove the current directory from your $PATH (that is, remove "." or leading or trailing colons)
The PATH value that is set is
Quote:
echo $PATH
/grid/common/pkgs/perforce/latest/bin:/grid/cic/common/bin:/home/shri/bin:.:/home/shri/bin:/grid/cic/ciccmweb/dfII/bin:/grid/common/pkgs/perforce/latest/bin:/grid/cic/common/bin:/grid/common/pkgs/cvs/v1.11.21/bin:/grid/common/pkgs/ccrtools:/grid/common/pkgs/pcrtools:/grid/cic/cvs/share/bin:/grid/common/bin:/bin:/usr/bin:/usr/local/mplayer/bin:/usr/local:/usr/local/bin:/usr/X11R6/bin
and if i unset this using
Quote:
setenv PATH
, it looses the meaning of every command. Then it does not know what is ls find..anything.
Can this problem be resolved somehow?
 
Old 01-28-2013, 10:45 AM   #4
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
I tried something like this also
Quote:
for file in *; do
if [ -d $file ]; then
cd $file; cp -rpf verilog systemVerilog
fi
done
but this seems wrong because i only want to copy verilog directory to systemVerilog
 
Old 01-28-2013, 11:33 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by shridhar22 View Post
Thanks colucix, this is the command that should do the work but i'm getting this message when try to execute it from the concerned directory(A)
What you get is a warning that suggest to remove the current working directory from your PATH environment variable, since it might cause a possible outbreak when used in conjunction with the -execdir predicate of the find command AND other unwanted situations. In other words it's always a bad idea to have . in your PATH. I think you can safely remove it, if you can determine where it's been added (at first look at your configuration files, like ~/.tcshrc, ~/.profile and so on, depending on the shell you're using).

In the meanwhile, to change it in your local running shell where the find command is supposed to run, you can simply reset the PATH omitting the dot (see the part highlighted in red):
Code:
$ echo $PATH
/grid/common/pkgs/perforce/latest/bin:/grid/cic/common/bin:/home/shri/bin:.:/home/shri/bin:/grid/cic/ciccmweb/dfII/bin:/grid/common/pkgs/perforce/latest/bin:/grid/cic/common/bin:/grid/common/pkgs/cvs/v1.11.21/bin:/grid/common/pkgs/ccrtools:/grid/common/pkgs/pcrtools:/grid/cic/cvs/share/bin:/grid/common/bin:/bin:/usr/bin:/usr/local/mplayer/bin:/usr/local:/usr/local/bin:/usr/X11R6/bin
$
$ setenv PATH '/grid/common/pkgs/perforce/latest/bin:/grid/cic/common/bin:/home/shri/bin:/home/shri/bin:/grid/cic/ciccmweb/dfII/bin:/grid/common/pkgs/perforce/latest/bin:/grid/cic/common/bin:/grid/common/pkgs/cvs/v1.11.21/bin:/grid/common/pkgs/ccrtools:/grid/common/pkgs/pcrtools:/grid/cic/cvs/share/bin:/grid/common/bin:/bin:/usr/bin:/usr/local/mplayer/bin:/usr/local:/usr/local/bin:/usr/X11R6/bin'
 
1 members found this post helpful.
Old 01-28-2013, 11:48 AM   #6
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks colucix , that worked for me thanks a lot. Could you please tell what
Quote:
-execdir cp -rpf '{}' systemVerilog \;
actually did? and the use of \;
 
Old 01-28-2013, 12:06 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You're welcome. As mentioned in my first post, details are in the man page. It is the specific syntax of -exec and -execdir predicates of find:
Code:
-execdir command ;

-execdir command {} +
       Like  -exec,  but  the  specified  command is run from the subdirectory containing the matched
       file, which is not normally the directory in which you started find.  This a much more  secure
       method  for  invoking commands, as it avoids race conditions during resolution of the paths to
       the matched files.  As with the -exec action, the ‘+’ form of -execdir will  build  a  command
       line to process more than one matched file, but any given invocation of command will only list
       files that exist in the same subdirectory.  If you use this option, you must ensure that  your
       $PATH environment variable does not reference ‘.’; otherwise, an attacker can run any commands
       they like by leaving an appropriately-named  file  in  a  directory  in  which  you  will  run
       -execdir.   The same applies to having entries in $PATH which are empty or which are not abso-
       lute directory names.
Basically the difference between using a (escaped) semi-colon and a (escaped) plus-sign is that the former causes the execution of multiple instances of the command, one for every matched file, whereas the latter causes the execution of a single instance on the matched files all together. In your specific case you need the \; form.
 
Old 01-28-2013, 01:07 PM   #8
shridhar22
Member
 
Registered: Mar 2012
Posts: 42

Original Poster
Rep: Reputation: Disabled
If i want to get a list of directories i.e. B1 B2 B3... that DO NOT contain verilog inside them. I need to move all such directories to somewhere else or i can tar them if it preserves all attributes like timestamp,etc. because after certain work i need those directories back
 
Old 01-28-2013, 03:46 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by shridhar22 View Post
If i want to get a list of directories i.e. B1 B2 B3... that DO NOT contain verilog inside them.
Assuming you consider only the first level of directories B1, B2, B3... immediately under the parent directory A, you can retrieve them by means of:
Code:
find /path/to/A -mindepth 1 -maxdepth 1 -type d -not -exec test -d '{}/verilog' \; -print
This prints out only those directories for which the test fails, due to the -not operator immediately before -exec.

If you want to move them to another location, use another -exec in place of the -print action:
Code:
find /path/to/A -mindepth 1 -maxdepth 1 -type d -not -exec test -d '{}/verilog' \; -exec echo mv '{}' /path/to/destination/ \;
Please notice that I've put an echo in the -exec statement for testing purpose. This will print out the resulting mv commands without actually run them so that you can review the results and avoid problems if something goes wrong or if they simply don't match your requirement.

By the way, instead of using a complex find command you can easily do the same with a simple loop:
Code:
for dir in /path/to/A/*/
do
  if [[ ! -d $dir/verilog ]]
  then
    echo mv $dir /path/to/destination
  fi
done
whereas the find command is useful when you have to span multiple levels of subdirectories recursively.

Last edited by colucix; 01-28-2013 at 04:19 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy a directory into another directory while ignoring specific directories or files wh33t Linux - Newbie 14 05-16-2012 09:13 PM
I need to tar a directory and then copy it to another directory locally wdsmith45 Linux - Newbie 7 02-07-2012 12:49 PM
How to Copy files from local directory to a particular directory using alias dynamics Linux - Newbie 7 09-11-2009 05:19 PM
How to copy files after search in array from this directory to another directory! phanvinhgiap Programming 4 01-04-2009 07:48 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 12:51 PM


All times are GMT -5. The time now is 12:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration