0
0
Linux CLIscripting~20 mins

cp (copy files and directories) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
cp Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this cp command?
You have a file named file1.txt with content Hello. You run the command cp file1.txt file2.txt. What will be the content of file2.txt after this command?
Afile2.txt will contain the text 'Hello'
Bfile2.txt will be empty
Cfile2.txt will contain the text 'file1.txt'
DThe command will fail with an error
Attempts:
2 left
💡 Hint
Think about what the cp command does when copying a file.
💻 Command Output
intermediate
1:30remaining
What happens when copying a directory without -r?
You have a directory named dir1 containing files. You run cp dir1 dir2 without any options. What will happen?
Adir1 is renamed to dir2
BThe directory dir1 and its contents are copied to dir2
Cdir2 is created as an empty directory
DAn error occurs saying 'omitting directory dir1'
Attempts:
2 left
💡 Hint
cp needs a special option to copy directories.
📝 Syntax
advanced
2:00remaining
Which cp command copies directory contents but not the directory itself?
You want to copy all files and subdirectories inside source_dir into target_dir without creating source_dir inside target_dir. Which command does this?
Acp -r source_dir/* target_dir
Bcp source_dir target_dir
Ccp -r source_dir target_dir
Dcp -r target_dir source_dir
Attempts:
2 left
💡 Hint
Using a wildcard copies contents inside the directory.
🚀 Application
advanced
2:00remaining
How to copy files preserving attributes and showing progress?
You want to copy a large file bigfile.iso to /backup preserving all file attributes (timestamps, permissions) and see progress during copy. Which cp command should you use?
Acp bigfile.iso /backup
Bcp -pv bigfile.iso /backup
Ccp -r bigfile.iso /backup
Dcp -p bigfile.iso /backup
Attempts:
2 left
💡 Hint
Look for options that preserve attributes and show verbose output.
🔧 Debug
expert
2:30remaining
Why does this cp command fail with 'No such file or directory'?
You run the command cp -r /home/user/docs/* /backup/docs but get the error cp: cannot stat '/home/user/docs/*': No such file or directory. What is the most likely cause?
AThe /backup/docs directory does not exist
BThe cp command does not support wildcards
CThe shell did not expand the wildcard because the directory is empty
DYou need to use sudo to copy files
Attempts:
2 left
💡 Hint
Think about what happens if the wildcard matches no files.