EzDevInfo.com

cp interview questions

Top cp frequently asked interview questions

Copying many files without stopping on errors on OSX

I need to copy several Gb from an external HD to my moan hd and some files will cause errors. If I do this with the finder, it will stop on the first error.

Is there a way to copy everything no matter the errors? Something like copy of teracopy in Windows?


Source: (StackOverflow)

Why does du -sl show different sizes for the source and result of a cp -rl?

I have used cp -rl to copy a folder. When measuring the size of the source and of the result of the copy du -sl returns slightly different sizes, even though diff confirms that their content are identical:

$ cp -rl folderA/ folderB/
$ du -sl folderA folderB
98561224 folderA
98590512 folderB
$ diff --brief -ra folderA/ folderB/
$

Both folders reside on the same hard drive, no modifications to any of them have been done between the copy and the measure. I found nothing in the documentation of du and cp which could explain the difference.


Source: (StackOverflow)

Advertisements

Is there a shortcut for unix `cp` to copy a file to the current directory with the same filename?

I'm hoping there's a shorter way of copying a file from a given directory to the current working directory than typing cp /path/to/file.txt file.txt

I tend to use this command a lot, and reiterating the file name seems... well... redundant.


Source: (StackOverflow)

Copying a file only when it is newer than the destination

How do I copy a file in Linux only when the file being copied is newer than the version at the destination?

If the file at the destination is newer, I want the file copy to not go ahead.


Source: (StackOverflow)

Copy file in GNU/Linux with progress bar and rate limiting

Is there any good tool in GNU/Linux that copy files like cp, but also shows progress and limits speed (and changes limit without interruption) like pv?

Prototype:

find source_directory | cpio -H newc -o | pv -s `du -bs source_directory/ | awk '{print $1}'` | (cd /destination/directory && cpio -di)

Also rsync -aP source_directory /destionation/directory/, but this shows progress bars individually and can't change rate after started.

Or may be I should just write a wrapper for pv/cpio? Done.


Source: (StackOverflow)

How to copy with cp to include hidden files and hidden directories and their contents?

How can I make cp -r copy absolutely all of the files and directories in a directory

Requirements:

  • Include hidden files and hidden directories.
  • Be one single command with an flag to include the above.
  • Not need to rely on pattern matching at all.

My ugly, but working, hack is:

cp -r /etc/skel/* /home/user
cp -r /etc/skel/.[^.]* /home/user

How can I do this all in one command without the pattern matching? What flag do I need to use?


Source: (StackOverflow)

How do you use regular expressions with the cp command in Linux?

I am attempting to only copy a subset of files from one directory to another using cp however am greeted with the message cp: cannot stat [^\.php]': No such file or directory. Is there a way to use regular expressions only using the cp command?


Source: (StackOverflow)

how to stop cp: overwrite './xxx' ? prompt

How can I stop the cp command from prompting to overwrite. I want to overwrite all the files with out having to keep going back to the terminal. As these are large files and take some time to complete.

I tried using the -f option. It still ask if I want to overwrite.

   -f, --force
          if an existing destination file cannot be opened, remove it and
          try again (redundant if the -n option is used)

cp -f /media/somedir/somefiles* .  
cp: overwrite `./somefilesxxx'? y

Source: (StackOverflow)

Reuse text on a bash command

If i'm writing a long command or just typing an extensive file path, is there any that i can "reuse" it with some command shortcut?

e.g:

1.cp /home/myuser/really/big/file/here/and/there.png /home/myuser/really/big/file/here/and/there.png.bkp

Do i really have to type it all over again?


Source: (StackOverflow)

How can I copy files with SSH?

I am trying to copy a file from my other computer, to another computer. (both running Ubuntu 9.10)

So, I've ssh'ed into the other computer; I cd to the directory; and I entered cp File.zip /home/me/Desktop as file.zip is located in the directory I just used cd with.

Now, it gives me the following error message:

cannot create regular file '/home/me/Desktop': no such file or directory

What do I have to do?


Source: (StackOverflow)

How do you copy directories in Linux in these situations using cp?

Situation 1

How do I copy /home/src/somedir to /dest/ such that the path always ends up as /dest/somedir, regardless of whether /dest/somedir already exists?

I want to avoid ending up with /dest/somedir/somedir.

Situation 2

How do I copy everything inside /home/src/somedir to /dest/ such that /dest/somedir isn't created and is ignored if it already exists?


Are there rules about the trailing slashes I can use? I thought ending with a trailing slash meant always copy the contents only but it still often goes wrong for me.


Source: (StackOverflow)

cp command to make bootable iso image usb

A lot of discussions exist here on making a bootable USB stick. Debian manual on installing Wheezy suggest the following:

# cp debian.iso /dev/sdX
# sync

Making sure the USB device is first unmounted.

Debian manual page.

I have done this in preparation to install wheezy. It seem to work, because the ISO image is all that is seen on the USB and usable size is that of the image. Remains to see if it will boot and so on.

Now, can somebody elaborate why this work. I thought a cp command like this is similar to a drag and drop, (apart from the USB being unmounted).


Source: (StackOverflow)

Cannot copy files from external USB HDD to computer

Hello I have a HDD disk connected with a USB converter to my computer. It consists of two partitions, the first one is mounted automatically and I can grab all the files from it, but the second one I have to mount manually as a root in the command line, if I try to open it with nautilus it gives an error.

The drive where the problem is is drive sdb1, sdb2 has the same settings but works fine. I am using Debian Wheezy.

This is the fstab:

/dev/sdb1       /media/usb0     auto defaults,uid=1000,umask=0777 0 0
/dev/sdb2       /media/usb1     auto defaults,gid=disk,umask=0777 0 0

And when I try to copy the files with this command (as root) cp -vr /media/usb0/* /home/user/Videos/ I get these types of errors:

cp: reading `/media/usb0/.lang/file.ext': Permission denied
cp: failed to extend `/home/user/Videos/.lang/file.ext': Permission denied

How can I at least copy the files to my main HDD? I don't need to adjust them I only need to copy them!

Update:


I got it mounted using my sudo, and the I can use the root folder, but I can't access one particular folder. Even when I use chmod -Rv 777 /home/user/external/.folder


Source: (StackOverflow)

Linux: Copy all files by extension to single dirrectory

I am trying to copy all *.tif files from ./old to the ./new. In ./old i have lots of subdirs with different files, and in ./new i need only TIF files, without folder tree. So, I had tried cp -vR ./old/*.TIF ./new and got an error:

No such file file or dirrectory "./old/*.TIF"

What I'm doing wrong?


Source: (StackOverflow)

Linux permissions and owner being preserved with cp

I can't understand the following behavior:
I have a file named someFile under /opt/com/internal/someFile
If I do ls -all /opt/com/internal/someFile the permissions are 700 user:userGroup

I am root and I do cp /root/folder/someFile /opt/com/internal/someFile

  1. I don't get a prompt to overwrite. Why not?

  2. The permissions are unchanged. But if I do cp /root/folder/someFile /opt/com/internal/someFile and the file someFile does not exist it is owned by root and not user. Why?


Source: (StackOverflow)