4

I am a beginner and I want to copy/move a file from a specific folder to the desktop. I use cp and mv for those tasks. But I want to copy/move to the desktop.

Say I want to move file1 to Desktop, I use

mv file1 _____

In _____,

  1. I used Desktop, but it renames the file to "Desktop".

  2. I used home/Desktop but it says no file/folder is found.

17

Desktop is a directory in your user's home directory.

If your language is English it is called Desktop.

To copy some file there, you can run

cp file ~/Desktop

~ stands for /home/<username>

If your UI language is not English, it is called differently.

You can do a copy without finding the correct name by

cp file "$(xdg-user-dir DESKTOP)"

You can get the name of the Desktop directory by

xdg-user-dir DESKTOP

You can use mv instead of cp if you want to move a file instead of copying.

| improve this answer | |
  • Thank you dude...By the way what this command "xdg-user-dir DESKTOP" actually implies? – adi Jul 10 '16 at 19:23
  • It shows the path to the desktop directory. You can run man xdg-user-dir for more info. – Pilot6 Jul 10 '16 at 19:50
8

How these commands work depends on where you are in the filesystem.

You can normally see where you are from your prompt:

zanna@monster:~/Desktop$ 

That's my prompt when the current working directory is ~/Desktop, the handy shortcut for /home/zanna/Desktop

If you're not sure where you are, you can type pwd and get the full absolute path

zanna@monster:~/Desktop$ pwd
/home/zanna/Desktop

The first / is important - that's the root directory, and all full absolute paths will start with it

You can use absolute or relative paths to do things with files. If you are in the directory where the file you want to move is, to move to your desktop, assuming your desktop directory is actually called Desktop (don't forget that Linux is case sensitive)

mv file1 ~/Desktop

because the current working directory is assumed.

From anywhere in your filesystem you can do this:

mv /path/to/file1 ~/Desktop

but replace /path/to with the real path! for example, if the file is in you home Downloads folder do

mv ~/Downloads/file1 ~/Desktop

mv also renames files... if the target is a file that exists and isn't a directory, mv overwrites it with the contents of the first file, and renames to the target. If the file doesn't exist, then file1 is renamed as the target without overwriting anything, as you discovered.

To copy a file instead of moving it, you can do exactly the same as above, with cp instead of mv. Only the behaviour is different in this case - the original file1 continues to exist in its previous location.

To learn more, you can check man mv and man cp

A nice option for learning - you can get mv and cp to tell you what they are doing by making them verbose: adding -v. Here I move the file chocolate from the current working directory ~/playground to my Desktop with the verbose option, and I get some output in the terminal:

zanna@monster:~/playground$ mv -v chocolate ~/Desktop
'chocolate' -> '/home/zanna/Desktop/chocolate'
| improve this answer | |
3

You should use mv file1 ~/Desktop for moving and cp file1 ~/Desktop for copying to your Desktop directory.

| improve this answer | |
  • If locale is not English, the name may be not Desktop. – Pilot6 Jul 10 '16 at 18:29
  • Good point. Any way to generalize to any locale? – edwinksl Jul 10 '16 at 18:31
  • 2
    @edwinksl Perhaps there's an XDG variable that points to it? wiki.archlinux.org/index.php/XDG_user_directories – Nick Weinberg Jul 10 '16 at 18:35
  • 1
    @adi FYI the ~ symbol is shorthand for /home/yourusername (for example, if my username is "Nick", ~/Desktop and /home/nick/Desktop are synonyms for the exact same directory) – Nick Weinberg Jul 10 '16 at 18:36
  • 1
    @Pilot6 It should work in any DE that conforms to the FreeDesktop.org specifications, which includes all of the major ones (and many of the minor ones), I think – Nick Weinberg Jul 10 '16 at 18:42
2

To move file1 from somedir to your desktop:

cd /somedir
mv -i file1 "$(xdg-user-dir DESKTOP)"

To copy the file instead:

cd /somedir
cp -i file1 "$(xdg-user-dir DESKTOP)"

The -i stands for "interactive." It will cause the move and copy commands to prompt you before overwriting any existing file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.