Linux Classes
Linux Training
* Linux Classes *

LINUX CLASSES - 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)

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 !!

*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 

Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.