Copying Files in Linux bash shell

So you want to copy a file in Linux? Easy enough.

cp filename newfilename

But sometimes it isn’t so easy… Say the file you want to copy begins with a .

You can’t see these files unless you add -a to your ls command

ls -la
.bashrc
.bash_profile

Once you can see the file, you can copy it as follows:
cp .bashrc .bashrc2

Not so bad, but if it starts with a hyphen it requires special handling to prevent your shell from thinking you are passing a parameter to the cp command. You’ll need to precede the filename with a ./ as follows:

cp ./–filename newfilename

Another difficulty with copying files is when someone has either intentionally or unintentionally created a file with a non-viewable ASCII character in the name. Say, for instance, someone has created a file with a name preceded by an ASCII space or the name is only an ASCII space. How do you delete that file? The same way as we copied a file starting with a hythen above.

rm ./ filename

you can also use rm -i to force you to verify the deletion for extra protection.

About vicki

Welcome to the Sovereign Republic of Vickistan. I am the President here. Lucky me! No taxes or laws yet. Lucky you!
This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *