|

Linux Files And Directories Commands

Linux Files And Directories Commands

The Linux files introduction episodes so far have been excellent for a gentle primer on how to get up and running with Linux using Ubuntu. Now we can get to some even more interesting challenges. In fact, that is how this Linux Series is designed to work. We start with the absolute basics, and move on to more challenging tasks. In this episode, we’ll be looking at copying, moving, creating, deleting, and overall just fully manipulating the files and directories on our Linux system. To do this we will make use of the four most commonly used commands in the Linux operating system. Those commands are cp to copy files and directories, mv to move and rename files and directories, mkdir to make new directories, and rm to delete files and directories. Let’s get to it.


Build A Linux File Sandbox

The best way to get used to these commands is to simply put them to use. Just like we do with our PHP, HTML, and JavaScript tutorials, we’ll create a sandbox which we can test out the things we learn. Let’s do that now.


vagrant@homestead:~$ mkdir sandbox
vagrant@homestead:~$ cd sandbox
vagrant@homestead:~/sandbox$ mkdir directory1 directory2
vagrant@homestead:~/sandbox$ cp /etc/passwd .
vagrant@homestead:~/sandbox$ ls -l
total 12
drwxrwxr-x 2 vagrant vagrant 4096 Feb 2 14:19 directory1
drwxrwxr-x 2 vagrant vagrant 4096 Feb 2 14:19 directory2
-rw-r–r– 1 vagrant vagrant 1735 Feb 2 14:20 passwd
vagrant@homestead:~/sandbox$ mv passwd password
vagrant@homestead:~/sandbox$ mv password directory1
vagrant@homestead:~/sandbox$ mv directory1/password directory2
vagrant@homestead:~/sandbox$ mv directory2/password .
vagrant@homestead:~/sandbox$ mv directory1 directory2
vagrant@homestead:~/sandbox$ tree
.
├── directory2
│ └── directory1
└── password

2 directories, 1 file
vagrant@homestead:~/sandbox$

In the snippet above, we complete several tasks. First, we use the mkdir command to create a directory named sandbox. Once we have created that sandbox, we use the cd command to move into that directory. Once there, we make additional use of the mkdir command. We actually created two directories in one shot. Here is just one simple example of where the command line is more powerful than the GUI. Using the GUI, you can not pass multiple arguments to a command to accomplish creating multiple directories at once like we do here. Next up, by using the cp command, we copy the the passwd file located in /etc to our current directory. Recall that the . indicates the current working directory. Using the ls command verifies to us that the copy is successful. Now we can test out the mv command. One must be aware of what they are actually doing with the mv command, since in some instances it will simply rename a file or directory, and in others it will move files or directories. Here we use it to rename the passwd file to password. Then, we use the same command, except to move password into directory1. We then move password from directory1 into directory2. Next up, mv is used to move password from directory2 back into the current working directory, which is sandbox. Lastly, we show an example of moving an entire directory into another directory. Finally, we confirm all of this with the tree command to see that in sandbox, we have directory1 which holds directory2, and we see that the password file itself is in the current working directory of sandbox.


mv Command Reference

Since there are a few ways to the mv command to use, it may be helpful to have a reference to document how the command works in different situations. Note the table here.

mv fileone filetwo Moves fileone to filetwo. In the case that filetwo exists, it will be overwritten with the contents of fileone. If filetwo does not exist, it will be created. Either way, fileone ceases to exist.
mv -i fileone filetwo By adding the -i switch to this command, the user will get a warning or prompt before anything gets overwritten. If you’re unsure, this is a good thing to do.
mv fileone filetwo directory1 This moves fileone and file two into the directory directory1. directory1 must already exist for this to work.
mv directory1 directory2 The result of this command will change depending on if directory2 exists already or not. If it does exist, the entirety of directory1 will be moved into directory2. If however directory2 does not yet exist, it will be created, then directory1 will be moved into it.

cp Command Reference

Likewise with cp, you’ll want to know what the expected result is of the commands you run.

cp fileone filetwo This would copy fileone to filetwo. If filetwo does not exist, it is created. If filetwo exists, it is overwritten with the contents of fileone.
cp -i Adding the -i switch will give you a prompt before overwriting any existing file.
cp fileone filetwo directory1 This will copy both fileone and filetwo into the directory directory1.
cp directory1/* directory2 By including a wildcard, all the files in directory1 are copied into directory2. You can also get granular, for example if you wanted only html files you would use *.html
cp -r directory1 directory2 By adding the -r switch this will copy the directory directory and everything in it, to the directory directory2. If the directory directory2 does not exist, it is created and will contain the same contents as the directory directory1.

Removing Files And Directories With rm

We have placed the rm command in it’s own section here for good reason. It is powerful, especially if you use it with wildcard operators. When we say it is powerful, what we mean is that it could be powerful in all the wrong ways if you’re not careful. This is because much like C Programming which also has this idea of direct access and power, Linux assumes you know what you are doing. If you type something like rm *.*, you can delete everything in one swipe. In Windows, it is assumed you might be dumb, so they give you a Recycle Bin so that if you delete something you weren’t supposed to, you can undelete it so to speak. No such process exists in Linux! So if you need to delete something with rm, make certain it is what you really want to do. For kicks, let’s test it out.

vagrant@homestead:~/sandbox$ cp ~/Code/Laravel/*.* .
vagrant@homestead:~/sandbox$ ls
composer.json CONTRIBUTING.md password readme.md
composer.lock directory2 phpunit.xml server.php
vagrant@homestead:~/sandbox$ rm *.*
vagrant@homestead:~/sandbox$ ls
directory2 password
vagrant@homestead:~/sandbox$

In the example above, we copied all files from the Laravel folder into the current working directory. When we list all the files, we can see that several new files now exist in our sandbox. We can see composer.json, CONTRIBUTING.md, readme.md, composer.lock, phpunit.xml, and server.php. Subsequently, if we want to simply delete all the files we just copied over, we use the rm command using asterisk dot asterisk notation to indicate we want any file with any extension to be deleted. A simple ls command does confirm for us that this is indeed the case.

Tip: When in doubt when using the rm command with wildcards, simply use the ls command first. This will tell you exactly what files will be affected. Once you’re confident which files you are working with, you can then use rm safely.

Wildcards and Globbing

Herein lies the power of working with files and directories directly from the command line. It is this ability to use wildcard characters to have an impact on many, sometimes hundreds of files at once. What we mean by globbing is the ability to target files using symbols like the asterisk for multiple strings of characters, or a question mark to identify a single character. It may help to also have a reference table of common combinations of wildcard characters and what they would accomplish.

*

matches all files

hey*

matches any file that starts with hey

yo*.html

matches any file that starts with yo followed by any characters and ending with .html

[xyz]*

matches any file that starts with either x, y, or z

TONES.[0-9][0-9][0-9]

matches any file that starts with TONES. followed by exactly four digits

awesome??

Matches any file that starts with awesome followed by exactly two characters

*[[:lower:]712]

matches any file that ends with a lowercase letter or the digits 7, 1, or 2

[![:digit:]]*

matches any file that does not start with a digit

[[:upper:]]*

matches any file that starts with an uppercase letter

These patterns share similarities to regular expressions. To learn more about how Regular Expressions work, check out our post that outlines learning about regular expressions.

Linux File and Directory Commands Summary

In this episode, even though we only investigated a handful of commands to work with files and directories, we were able to accomplish a wealth of tasks. That is how Linux works. By knowing these commands well, you can get a lot of mileage out of them.