Wednesday, 1 June 2011

Create, delete, copy and move files and directories

We are still looking at the basics here. You can get an idea of what the command options are by using man commandname or commandname --help. You might discover funky options available to you, who knows, strangest things have happened.

Create:
  • File: touch filename, this will create an empty file. Alternatively, use your favourite text editor.
  • Directory: mkdir directoryname this will create an empty directoryI like the -p switch, as this will create the entire path for you in one neat command, e.g. mkdir -p /firstdir/seconddir/thirddir
Delete:
  • File: rm filename. There are many switches for rm, I tend to use rm -f filename so that I don't get prompted every time. Use with care though, particularly when deleting directories.
  • Directory: rmdir directoryname if directory is empty. rm -r directoryname if it isn't. You can add the -f switch to delete without being prompted
Copy:
  • File: cp myfile mydir/ There are a myriad of options for cp. You can have a look at them with cp --help or better still have a look at the man page man cp.
  • Diretory: cp -r sourcedir/ destdir/
Move:
  • File: mv sourcefile mydir/ Note that mv can be used to rename a file, if the source and destination are on the same directory, mv test test2. As with cp, mv has quite a few options. Also, note that you don't need the slash you could type mv sourcefile mydir
  • Directory : mv sourcedir destdir Again, it can be used to rename directories too.

No comments:

Post a Comment