LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 11-09-2006, 04:37 PM   #1
jstars
Member
 
Registered: Jun 2005
Location: Edmonton, Canada
Distribution: SuSE 10
Posts: 51

Rep: Reputation: 15
Help with "cp" command syntax


I have a directory with many sub-directories, each containing flies. I would like to copy all of these files (but not the directories) to another directory.

My attempts at recursively doing this with "-R" as well as "! -type d" does not work.
 
Old 11-09-2006, 05:05 PM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Ok... let me see if I understand what you're trying to do...

You want to copy the non-directory contents of a tree of directories... for example, given the following tree:
./a/b/file1
./a/file2
./a/b/c/file3

you want file1, file2, and file3 to be copied elsewhere without maintaining the directory structure.

You CANNOT do this with a single cp command - copying files recursively without maintaining the directory structure doesn't really make sense. Still, it's not an impossible or difficult task - you could do it in one line with find.

Code:
find DIRECTORY_TO_COPY -not -type d -exec cp -d -i "{}" DESTINATION_DIRECTORY \;
Obviously, by removing the directory structure, some files may have the same name. Using the -i flag for cp (as above) will have cp warn you if this happens. The -d flag will have cp preserve symbolic links.

See man cp.

See man find.

Last edited by zhangmaike; 11-09-2006 at 05:11 PM.
 
Old 11-09-2006, 05:13 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by jstars
I have a directory with many sub-directories, each containing flies. I would like to copy all of these files (but not the directories) to another directory.

My attempts at recursively doing this with "-R" as well as "! -type d" does not work.
For reasons already noted---and probably some others--I think it will be far better to copy the whole structure--sub-directories and all. For most things, the extra sub-directories can't really hurt antyhing, and you can always do more "clean-up" later.
 
Old 11-09-2006, 06:14 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If you really want to copy the files from all sub-directories to a ginle directory, try this:
Code:
find /path/to/src/dir -type f -exec cp -i "{}" /path/to/dest/dir \;
The -i option to the cp command will make it interactively ask before over-writing files with duplicate names. Clearly this is not suitable for operation inside a non-interactive script, but it should give you some safety in case there are files with the same name within your hierarchy of source directories..
 
Old 11-09-2006, 07:02 PM   #5
jstars
Member
 
Registered: Jun 2005
Location: Edmonton, Canada
Distribution: SuSE 10
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks, I'll give it a try. Admittedly I didn't think about the dangers of filenames with the same name.

A poster above opined that it didn't really make sense to do this operation. There is a practical reason why I'm doing it: The sub-directories contain images. For presentation purposes I want to place all images in one directory so that they can be viewed sequentially would having to navigate between directories.
 
Old 11-09-2006, 07:24 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,445

Rep: Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068Reputation: 1068
In which case you might find "mv" a better option.
Or mmv even better if you have it on your distro.

Multiple copied of images can eat disk space real quick.
 
Old 11-09-2006, 07:31 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by jstars
For presentation purposes I want to place all images in one directory so that they can be viewed sequentially would having to navigate between directories.
Another approach for doing this would be to leave the original files where they are, and create a directory full of symlinks to them. I've done this myself actually

You can generate the filename with a sequentially numbered prefix so it doesn't matter if there are duplicate names:

Code:
$ mkdir picture_links
$ cd picture_links
$ n=1
$ find /my/src/directory -type f | while read f; do ln -s "$f" "${n}_${f##*/}"; let n+=1; done
The most cryptic bit here is the ${n}_${f##*/}. The ${f##*/} is a way to get the file name without the directory name. This is sometimes referred to as the basename of the path. There is a program which ships with most Linux systems to do this, but this method is an internal shell feature which is much quicker to call than invoking a separate program for each file processed. See the "Parameter Expansion" section of the bash manual page for more details.

The expansion of the n variable uses the braces because id we wrote this:
Code:
$n_${f##*/}
...the shell would try to expand the variable named n_ (which would expand to an empty string since this is probably not defined).
 
  


Reply

Tags
cli, cp, recursive, syntax


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
./configure script hangs at "checking for ICMP ping syntax..."l khilari Linux - Software 4 11-12-2011 11:27 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 12:49 PM
Tiny Sofa 2.0 - I thought "halt", "reboot" were only root command ?? sorcerer Linux - Distributions 1 08-21-2004 04:28 PM
"segmentation error" when issuing "useradd" command through terminal with RH 9.0 kaihuang Linux - General 0 10-22-2003 12:47 AM


All times are GMT -5. The time now is 03:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration