linux cp: how to have it follow links but not stop if a link target doesn't exist - linux

I want to recursively copy a dir and have the targets of the links copied, but I do not want the cp to stop if a target of a link does not exist.
For example, I run this command:
cp -fprL /path/to/src_dir /path/to_dest_dir
But the first time it hits symlink where the target doesn't exist it exits:
cp: cannot stat `/path/to/non-existent/file': No such file or directory
Is there some way to get cp to silently skip these and continue on?

With the standard GNU toolchain, no, there's no way.
You could instead copy your files, keeping symlinks as symlinks, then use find -follow -type l -delete to delete the broken symlinks, and then copy again, this time following symlinks.
Of course, you could also just write a python etc. program to do the copy for you, or find all files in the original trees that are not broken symlinks and use these with cp, replacing parts of the path with the target path using sed:
find -type d|sed 's/^\(.*\)/"\1" "\/target\/\1"/g'|xargs -p mkdir
find -follow -not -type l -not -type d|sed 's/^\(.*\)/"\1" "\/target\/\1"/g'|xargs -n2 cp
sed will duplicate your found file path, prefixing it with the target directory.

Related

copying files from etc ending with digit to test1 directory

I'm new to linux and as an exercice I need to copy the "etc" files that end with a digit from home directory to the test1 directory
(with one command).
I tried this but it dosn't work
find /etc -type f -iname "*[3-9]" -exec cp {} ../test1/ \;
this should work for your home directory files ending with digit
mv `ls . |grep -Eo "^.*[0-9]$"` your-directory
lets says in the current directory you have some files like ofjweifhwef9 or kfhiofeh8 ( files ending with digit)
so ls will list them.
this grep expression "^.*[0-9]$"` will find only files ending with digit. ( because in your home directory system wont allow to have a file like this "/etc/somefile123")
and then mv will move those files to your-directory
note :- if grep cannot find the files ending with number you will see an error ofcourse because mv needs 2 operands but since it wasn't there so error.
mv: missing destination file operand after './your-directory'
It is probably because /etc is a link in the system that you're using, and find doesn't seem to consider it a path until you add an extra / at the end. Try this instead:
find /etc/ -type f -iname "*[3-9]" -exec cp {} ../test1/ \;
Notice the /etc/ instead of /etc. I get the same behavior on my Mac where /etc is a link to another directory.
Of course, also make sure that you have files which names end on a digit under the /etc/ directory tree. I have none in my mac. You should get some files when you run:
find /etc/ -type f -iname "*[3-9]"
If you don't, you don't have any files to copy. You may also try: find /etc/ to see all files under the directory tree.
Finally, you may want to add the option: -depth 1 if you only want to copy the files in the /etc/ directory, as opposed to all the files that match in the directory tree under /etc/.

Recursive find and copy to other directory

I need to find all files in a directory and it's subdirectories, but I need to keep directory structure. For example there is a file
/media/subdir1/subdir2/file.jpg
and I want to copy it to
/new-media/subdir1/subdir2/file.jpg
and the same to all files inside /media/ directory. And by the way, directories inside /new-media/ must be created if not exist.
if I use
find /media/ -name '*.jpg' -exec cp /new-media/ ????? {} \;
how can I get all subdirectories inside /media/?
The above will get you everything in /media, but to get exactly what you want you probably want to use something like:
Method 1: Copy only what you need recursively, per your requirements:
mkdir ../media2; find . -name "*.jpg" -exec cp -r --parents {} ../media2 \;
I ran this from inside the directory you want to search recursively. It does a couple things:
1 - create the new destination directory
mkdir ../media2
2 - then finds all files ending with ".jpg" recursively.
find . -name "*.jpg"
3 - uses -exec to pass the copy command to each file returned to find as a match, and subs that in as the first argument (which with the syntax of cp, is going to be your source file):
-exec cp -r --parents {} ../media2 \;
the "--parents" flag retains existing directory structure and recursively creates subsequent parent directories. Super useful right?
Method 2: there might be a better way to do this with xargs, but the above ended up being the most simple method imho. That said, if you want to think outside the box, you could simply copy the entire directory over recursively, then remove anything NOT ending with ".jpg" with something like:
cp -r media media2; find ./media '!'-name "*.jpg" -type f | xargs rm
I think this is what you want:
cp -r /media /new-media
-R, -r, --recursive
copy directories recursively
Hope this helps.

Newbie-ish error: cp: omitting directory

Pulling my hair as I'm stuck with a basic error without understanding why:
find . -type f -exec cp del {} \;
We're in a "test" directory, in which I created one "del" subdirectory. The "test" directory contains a variety of files of various types.
The result is a series of lines (same number as the number of files present in the directory from where the command is ran) with:
cp: omitting directory `del'
Possibly useful details follow.
Debian Wheezy, standard shell interface.
As a prelude to more complex exclusion and exec patterns I wanted to start with this fundamental test... and had this.
I think I excluded the "del" directory with "type -f", so it's not as if I was asking Linux to move a directory within itself.
There are no other directories or subdirectories.
Permissions: everything belongs to the current user.
I made variations for the "cp del" part, putting it in simple or double quotes, or using ./del, no difference.
I also tried with -R
find . -type f -name '*script1*' -exec cp -R ./del {} \;
That gave:
cp: cannot overwrite non-directory `./script1' with directory `./del'
Same with -r
If what you're trying to do is to copy some files found by find command to the del directory, then you can do it like this:
find . -type f | xargs cp -T del/
Or like this:
find . -type f -exec cp {} del \;

Linux: copy ".svn" directories recursively

I know there are dozen of questions about similar topcis but I still can't beat this up.
I need to copy all .svn directories recursively from /var/foo to /var/foo2 on a Debian machine:
/var/www/foo/.svn
/var/www/foo/bar/.svn
...
I tried these two commands without success:
find /var/foo -name ".svn" -type f -exec cp {} ./var/foo2 \;
find /var/foo -name ".svn" -type d -exec cp {} /var/foo2 \;
Once only the svn directory right inside foo is copied, while another time nothing is copied.
Given following file structure:
./
./a/
./a/test/
./a/test/2
./b/
./b/3
./test/
./test/1
Running following script in the directory to be copied:
find -type d -iname test -exec sh -c 'mkdir -p "$(dirname ~/tmp2/{})"; cp -r {}/ ~/tmp2/{}' \;
Should copy all test directories to ~/tmp2/.
Points of interest:
Directories are copied to the destination on a one-by-one basis
Parent directories are created in advance so that cp doesn't complain about target not existing
Rather than just cp, cp -r is used
The whole command is wrapped with sh -c so that operations on {} such as dirname can be performed (so that the shell expands it for each directory separately, rather than expanding it once during calling the find)
Resulting structure in ~/tmp2:
./
./a/
./a/test/
./a/test/2
./test/
./test/1
So all you should need to do is to replace test with .svn and ~/tmp2 with directory of choice. Just remember about running it in the source directory, instead of using absolute paths.
I find that using tar for such operations makes the code often much more readable:
$ mkdir /var/www/foo2
$ cd /var/www/foo2
$ find ../foo/ -type d -name .svn -exec tar c \{\} \+ | \
tar x --strip-components=1
find will list all directories named .svn, and call tar to create (c) an archive file (that is sent to stdout) with all these directories. the archive on stdout is then extracted (x) by another tar instance in the target directory. the relative path portion (../) is automatically removed by the archiving tar, but since we also want to remove the first path component (foo/) we need to add --strip-components.
Note: This will only work if you do not have very many .svn directories you want to copy (more than $(getconf ARG_MAX)-2, which on my system is more than 200000).

Edit the contents of directory tables (Linux)

How can I edit the contents of a directory file? Since directories are just special files with a table of links to the files contained within, I should be able to view the table right?
When I open a directory in vim I can't go into insert mode to edit the links, which I assumed would be the basic available functionality, even if it didn't work.
Like Kevin said, you can't do it. What you CAN do is see symlinks and hardlinks with find, and I suppose use the ln command to do what you want from there.
Using the find command
You can see all normal files with this command...
find <DIR> -type f -links 1
You can see hardlinked files with this command...
find <DIR> -type f -links +1
To see all symlinks, you could use...
find <DIR> -type l
You can add -ls to any of these to get a more verbose output.
Finding all symlinks in current directory, non-recursively, with additional file info:
find . -type l -maxdepth 1 -ls
Using ln and unlink commands
Use ln to create links...
ln <current_file> <link_filename>
...and for symlinks...
ln -s <current_file> <shortcut_filename>
..and of course to delete a hardlink, use rm as usual...
rm <hard_link_filename>
...and for deleting a symlink....
unlink <symlink_filename>
Note: if the symlink links to a directory, be sure not to include a final "/"
This will not work.... unlink /var/www/deleteme/ if deleteme is a directory on the other end.
Hope this helps.

Resources