Linux Basics: Creating, Moving, Renaming and Deleting Files & Directories

May 22nd, 2008 · No Comments

Ok it’s time for a new Linux Basics Commands tutorial! Today we’re going to see some basic commands that allows you to create, move or delete files and directories in a Linux shell.

While some of you may prefer to use a GUI like Gnome, it’s always nice to learn this basic stuff. Sometimes you don’t have remote access to your desktop or the X server is crashed so you better know some shell commands.

Creating And Removing Directories On Linux

The linux command to create a directory is mkdir. The syntax is pretty:

mkdir [directory]

So if you wanted to create a directory named “documents”, you would simply type:

mkdir documents

To verify that the directory has been created successfully, simply type ls to list all files and directories.

Now to remove a directory, the command is rmdir. The syntax is the same as mkdir, so to remove a directory named “documents”, you would type:

rmdir documents

A directory must be empty before it is removed using rmdir. So how do you remove a non-empty directory? Well you would have to use the rm command and force it’s execution. Let’s pretend we want to remove the “documents” directory which isn’t empty:

rm -f documents

In this case, the “f” switch forces the deletion of all the directory’s content. If you didn’t use the “f” switch, then you’d have to confirm the deletion of each file one by one.

Deleting Files

The command to delete a file is rm. The syntax is:

rm [filename]

So if you wanted to delete a file named mydocument.txt, you would type:

rm mydocument.txt

If you want to delete multiple files, you could use a wildcard:

rm *.txt

This would delete all files with the .txt extension. Just as with directories, you can use the “f” switch to force deletion so you don’t have to answer yes for each file to delete:

rm -f *.txt

Moving and Renaming Files and Directories

Moving files or directories on a Linux box is achieved using the mv command. The syntax is:

mv [source] [destination]

So let’s say you want to move the “documents” directory to the “tmp” directory (assuming it already exists), you would type:

mv documents /tmp/

This would move the documents folder into /tmp (ie.:/tmp/documents).

Now how about renaming the “documents” folder to “mydocuments” (assuming “mydocuments” does not exist!). You would use the mv command this way:

mv documents mydocuments

Pretty easy huh? The same thing would apply to a file instead of a directory.

Want more tutorials on using basic Linux commands? Subscribe to my feed by email!

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment




Posted in Linux · Tutorials | No Comments

Dedicated Servers
 
VPS
Website Hosting
 

Recent Comments

Recent Webmasters

Hosting Type :
Monthly Price :
Storage :
Transfer :
Sort By :