Linux Classes
Share This With a Friend  
LINUX CLASSES - FILES

Linux Copy and Rename Files

How Do I Copy and Rename Linux Files?

Copying Files

Copying files under Linux is similar to copying files under DOS. Here's an example using the cp (copy) command: $ cp goulash recipes/hungarian
$ cp stuff stuff.bak

The first example copies the goulash file from the current directory and stores it in your recipes/hungarian directory. The second example creates a file called stuff.bak in the current directory that is identical to the source file stuff.

Note: The cp command will overwrite files with the same name without warning. To be prompted before overwriting, use the -i flag, like so:

$ cp -i goulash recipes/hungarian
cp: overwrite recipes/hungarian/goulash (y/n)?

Renaming Files

Use the mv command to rename a file or move it to another directory, like so:

$ mv stuff junk
$ mv junk trashcan

The first example renames the file stuff as junk, and the second moves the file junk into a directory called trashcan.

Note: The mv command will wipe out files without warning. To be prompted before overwriting, use the -i flag, like so:

$ mv -i stuff junk
mv: overwrite junk (y/n)?

Previous Lesson: Displaying Linux Files
Next Lesson: Creating Files and Directories

[ RETURN TO INDEX ]


   

Comments - most recent first
(Please feel free to answer questions posted by others!)

justin     (28 Jul 2013, 19:17)
How do I copy a file regardless of case (ie. Pp Ss Ps Sp....)
Mohamed     (24 Mar 2013, 02:22)
Hi
there is something confusing me here, in the MV command.
how is the first example rename file and the second move files ?? they are both the same "rename files"

shouldn't the 2nd example has an absolute location ?
Copy zip files from one dir to another     (01 Feb 2013, 22:44)
[oracle@localhost media]$ cp /media/sf_TEMP/*.zip/ /oracle/home/media

cp: cannot stat `/media/sf_TEMP/*.zip/': No such file or directory

oracle@localhost media]$ ls -l
drwxrwx--- 2 root vboxsf 4096 Feb 1 17:23 sf_TEMP

[oracle@localhost media]$ touch sf_TEMP
[oracle@localhost media]$ cd /sf_TEMP


[oracle@localhost media]$ uname -r
2.6.32-300.39.2.el5uek

What can't I copy the file.
I log out and back into the terminal, to no avail.

Please help
Sammy     (14 Apr 2012, 00:21)
@Chethan you probably need to put sudo before the command
Sammy     (14 Apr 2012, 00:20)
Guys you need to add -r after cp to copy a directory
chethan     (05 Apr 2012, 11:42)
hi dr., i am new to linux,1:how can we copy a file from user to root
Leila     (24 Mar 2012, 23:13)
I just wanted to compliment you on your tutorial, great job! Also wanted to comment on the fact that you seem to get a lot of questions on here. You are very patient, thank you.
tabish     (29 Feb 2012, 04:41)
Hi sir , m learnin perl , i wrote a script and saved it as a.pl , then i typed
"chmod u+x a.pl " in d terminal , but all i get is " d file dsnt exist" ...
Ive checkd d path n evrythn !!
tabish     (29 Feb 2012, 04:20)
Hi sir , m learnin perl , i wrote a script and saved it as a.pl , then i typed
"chmod u+x a.pl " in d terminal , but all i get is " d file dsnt exist" ...
Ive
Rob Bankin     (28 Feb 2012, 15:20)
Please send me the Linux tutorial.

Thanks.
akansha     (26 Feb 2012, 03:30)
Whenever I format my system, I take backup of my HDD in my external HDD. After installationt it becomes a tedious task to manage all the files in a proper manner. I just want to write a Shell script which creates a backup of all my files inside an external USB drive in a structured manner.
Structure:

Backup
txt
pdf
doc
avi
mpeg
...
...
etc...

1. Script should create a directory named as BACKUP inside the external HDD.
2. Inside the BACKUP directory it will start creating more directories according to the file extentions.
3. Backup of all the files will be created according to the extension of file.
vmedaq     (01 Feb 2012, 08:39)
I wanted to make a .bin file which will copy all the files with name having "am" in common... usually in the terminal we use "cp *am* "... I wrote the same command inside the .bin file, a shown below... but it acts like "echo"... please help.

file name "copier.bin"
------------------------------------
cp *am* /home/vmefiles/
-----------------------------------
then I did "chmod +x copier.bin
and finally "./copier.bin" but the out put is just:
"cp *am* /home/vmefiles/"

please help...
Ambika Singh 007     (23 Dec 2011, 06:41)
While Copying file name in which it is copied must be present.
But while moving/renaming there is no such need
Kundan     (28 Nov 2011, 23:15)
Thanks a lot..
sameh     (26 Nov 2011, 15:23)
hello i need some help please , i have zip file the extention .tar.gz i cant copy it to SD Card. the message error is permission denied.
Jim     (25 Nov 2011, 19:14)
Can I get a copy of the goulash recipe? Seriously. :)
cindy     (05 Oct 2011, 20:12)
hi. guess i know even less then i thought i did. and i didnt think i knew much..... problem is i have years of using windows, and after a recent computer virus infestation, my friend cleared out my computer and changed the thing to linux.i am slowly oh so slowly learning to use it. i can get on line, but i have NOT figured out how to download anything (for example games or documents). well maybe i download them but cant find something that would open them i guess. can you help ? thank you
eduard chavez     (28 Jul 2011, 00:54)
how to rename file to ubuntu
SimpleFix     (02 Mar 2011, 00:35)
typing

"man rename"

into a terminal reveals that you can simply type

rename 's/OriginalNameChunk/ChunkToReplaceOriginal/' *.FileExtension

johnmarshall@mailcatch.com     (02 Jan 2011, 09:46)
I program in DOS but can't seem to learn to do the simplest things in linux on my netbook

In DOS I would

rename MOV*.* *.mpg

and do the complete directory in two ticks.

In linux it seems impossible without two hours of coding.

Looked everywhere on the net and still can't find a SIMPLE example of renaming an existing file with no extension with an extension of my choice.

Bugger.

Can u give one for the other millions of people wanting to do it?
John
Jose     (26 Dec 2010, 04:59)
I want to rename existing files, but only if they're different...
Does someone know something better than 'cp -rbV number Folder1 Folder2' ?
mustafa     (29 Nov 2010, 01:18)
Using shell scripts, simulate the following commands

1)
• CP
• CD
• Create File
• Copy file contents into another files
• Create User
• Delete User
• Login using GUI.
leavie     (04 Nov 2010, 22:29)
how to rename multiple files in linux???
spanien mietwagen     (22 Oct 2010, 05:39)
Very enlightening and beneficial to someone whose been out of the circuit for a long time.

- Kris
Manan Jadhav     (20 Oct 2010, 01:45)
Hey would it possible for you to tell me how to simulate this command in shell script..
jabz     (25 Sep 2010, 16:54)
I want to copy two files to a directory at the same time what command would do this anyone please help, i am just new to linux
Nicky     (11 Sep 2010, 17:32)
trying to copy a file from /etc directory to my present working directory, and renaming it file4 at the same time.
cp /etc/p2/rename /home/f4
Trying to copy p2 to w3 directory in my home directory, current wk dir, renaming p2 to f4
( No such file or directory)
kasi     (18 Aug 2010, 06:51)
it is very very useful doctor.....
Bob Rankin     (23 Jul 2010, 08:42)
@loge - No way that I know of, sorry.
loge     (23 Jul 2010, 07:27)
is it possible to get the old name of the file after renaming it in linux
supem00b     (01 Jun 2010, 22:10)
@ben
You spent two hours trying to copy files? Perhaps you should be skiing or playing football. That's funner... EPIC LULZ!
ben     (01 May 2010, 21:55)
i spent two hours trying to copy my files and nothing i give up back to good old windows 7 4 me
bash_noob     (25 Apr 2010, 00:34)
also as specified in "Deleting Files and Directories" section use it cautiously....
bash_noob     (24 Apr 2010, 23:40)
@ja
use the r switch
it also works with most other file-only commands
Bob Rankin     (23 Apr 2010, 07:08)
@hm - Simple typo. You either typed "linus" instead of "linux" or "dara" instead of "data".
h m     (22 Apr 2010, 22:19)
I'm trying to copy "/usr/local/linuxgym-data/vimdata/wordlist.txt FILE into the ch2-vim DIRECTORY.

I type: cp /usr/local/linuxgym-dara/vimdata/wordlist.txt ch2-vim

and error shows up saying
cp: cannot stat `/usr/local/linusgym-data/vimdata/wordlist.txt': No such file or directory

what could be wrong?
jay     (20 Apr 2010, 20:22)
rem to state the entire directory listing for destination. e.g. mv index.html /home/www
ja     (08 Apr 2010, 00:16)
why i cant copy a folder into a folder too?? the errors said:

omitting directory.
ja     (08 Apr 2010, 00:12)
Why i cant copy my file to the other folder??

i just type:

cp ja Desktop/folder1

ghost     (06 Apr 2010, 07:57)
To use the same name for a file as for an already existing directory (sort of), I would first give the file an extension to distinguish between the two.
eg. you have a file called 'stuff' and want to rename it 'junk' to put into the folder 'junk'.
1.) mv stuff junk.bak
2.) cp junk.bak junk

*I guess*
Bob Rankin     (13 Mar 2010, 08:47)
You can't have a file and a directory with the same name.
RickRasta     (12 Mar 2010, 03:23)
How would you rename a file with the same name as a directory?
Bob Rankin     (01 Mar 2010, 06:43)
If trashcan did not exist as a directory, it *would* rename junk.
Dennis     (01 Mar 2010, 03:36)
$ mv stuff junk
$ mv junk trashcan

How the system knows not to rename "junk" with name "trashcan",as it does in first case, but to move it in directory.
Bob Rankin     (22 Feb 2010, 05:27)
What is the exact command you are entering?
m3m3     (20 Feb 2010, 05:29)
I checked that & it was there.
Is there any other error I could have done??
is there any other thing I must consider??
Bob Rankin     (19 Feb 2010, 10:32)
Perhaps the target folder does not exist?
m3m3     (19 Feb 2010, 09:40)
I used the steps below but I dont seem to manage to work it out , I get this message" no such file or directory" & the file is right infron of my eyes!!
do I have to change the directory to somewhere else??
sheemul     (08 Feb 2010, 05:03)
Excellent !!

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.