How to Manage Files and Directories in Linux – A Quick Reference
Posted on Updated onIf you’re coming from a Windows environment and are new to Linux, you’ll have to get used to using the command line a lot. Despite there being several Linux distributions that focus around a GUI, the majority of Linux work still gets done from the CLI (Command Line Interface). In fact, the GUI apps are mostly just wrappers around the command line anyway. Moreover, often you simply won’t find a GUI on most Linux boxes. The reason is that a GUI environment takes away precious resources on a server that would be better spent elsewhere.
Even if you’re used to the DOS prompt on Windows (like me), moving to Linux can cause a bit of confusion as the commands are all different, and there are a few new concepts to wrap one’s head around. Taking all this into consideration, here are a few commands to get you started and learn how to manage files and directories in Linux.
Listing Files
I’ve actually written a more detailed tutorial on finding files in Linux from the command line. It goes into a lot more detail and even shows you how to order the results and sort them by “last accessed”, “last modified” etc. But listing files is simple. In DOS we use the “dir” command. In Linux, it’s just:
ls
This will give you a list of files and directories in your current directory. Depending on the way in which you’re accessing Linux, it might even color code the directories separate from the files. For example, if you access your box via SSH and Putty, the directories will be blue in color.
You can follow “ls” with a directory name and it will list the files in that directory instead of your current one. Like this:
ls /username/bin/
Renaming and Moving Files
It might seem odd to group “renaming” and “moving” together, but conceptually they’re the same thing. Renaming a file is the same as duplicating a file using a different name, and then deleting the original. In Linux, the “mv” command reflects this as you use the same command for renaming and moving. For example, to rename a file in your current directory simply use:
mv oldfilename.xyz newfilename.abc
While this looks as if you’re recreating the file with a different name, it’s really just renaming it. And since renaming and moving are the same in principle, you can use the same syntax to move files from one location to another using this:
mv filetomove.abc newfolder/locationofnewfile.xyz
Just specify the final location with the directory name in the second argument, and the file will be moved to the new location.
Copying Files
If you want to copy files instead of moving them, use “cp” rather than “mv”. The syntax is exactly the same – the only difference is that the original file will continue to exist. So to duplicate “file1” and call it “file2”, the following syntax will work:
cp file1 file2
It’s easy!
Deleting Files and Directories
The “rm” command removes files or directories. To remove a file, just use:
rm filetoberemoved.abc
To delete a directory, use the “-r” argument:
rm -r [directory to be removed]
The “-r” specifies recursion. The command will remove all files, subdirectories, and the main folder itself. It doesn’t ask for any confirmation, so be careful!
Current Folder and Navigating Back to your Home Directory
Unlike DOS, Linux doesn’t always give you the clearest indication of where you currently are. To find out where you currently are, type in:
pwd
And to go back to your home folder, simply type in:
cd
Creating New Files
Finally, creating a new file in your current folder is easy as pie. Simply use the following syntax:
> newfile.abc
Here’s a screenshot of the new file being created:
While there’s a lot more to learn about Linux commands and even basic file management and navigation, these are great starting points that I wish I had known about earlier. I hope you find some it useful!