In Part I of this article, we became acquainted with Terminal, the window for entering commands in Linux Mint and learned how to use the built-in help for Linux commands.
In a previous article, we created some sample text files in our Documents folder. Let's use those to experiment with copying and moving files around. If you didn't do that step and need to learn how to make some files, please do so and then continue.
How to create directories (folders)
First, we will make a couple of new folders in our Home folder; if we mess anything up, it won't be tragic since they are just for learning. To make a folder, or directory in computer jargon, we use the mkdir command.
We can make a directory anywhere, by specifying its full path from the root folder such as /home/yourusername/ImportantFolder/Subfolder1/OtherFolder/LastFolder and using the -p option (which means to create parent folders as needed). If all of those folders in between yourusename and LastFolder don't exist, Linux will create them all at once with just that one command! That's a time saver.
However, most of us just make subfolders, or child folders, in the directory we are currently in. In that case, we don't need to supply a path, just the name of the new folder. Start Terminal from the main Menu or by clicking its launcher on your desktop. So go ahead and use cd ~ to navigate to your Home folder. Then type mkdir testfolder1 and press Enter.
Nothing happened? Well, it did but Linux doesn't brag about its successes, it only reports back to you if there was an error. If you type ls, you will see your new folder listed.
Try it again, but this time type mkdir -v testfolder2 and press Enter. The -v option tells Linux to be “verbose” and tell us exactly what happens, so you will see a confirmation that it did indeed create the new folder. If you type ls -l, to show the detailed style of listing, you will see that Linux automatically assigned the folder to you as the owner and to your personal group.
In the last article, you created some sample text files which we used when getting acquainted with Nemo, the file manager of Linux Mint. Let's work with them some more now.
Change directories to your Documents folder, by typing cd Documents and pressing Enter (I will omit the instruction to press Enter after every command from now on to save a bit of time and writing). Let's look for your files by giving ls a pattern to search for. We use the asterisk symbol to tell Terminal to match any characters or numbers where the asterisk is positioned in what we are searching for (our search string). We're looking for one or more files that have “sample” in the name, so if we search for sample*, Terminal would find sample.txt, sample1.doc, samplefile.txt and so on on. If we thought we might have misspelled the first character, we could search for *ample* and it would find those plus example.doc and MyExample.txt as well. Enter ls sample* and you should see your files listed.
How to copy files
Now let's copy them to the test directory we just created. We type the copy command, then the source file (the file to copy), and finally the target (the destination folder/filename). If we want to copy it as the same name, we just specify the target folder; it we want to copy it as a different filename, we have the option of adding the new name to the target folder.
So, enter cp sample-1.txt ~/testfolder1, remembering that the tilde represents your Home folder. Terminal just returns you to the prompt; it copied the file but assumed you knew that since it showed no error message. To make it more chatty and to prevent mixups, we can add two options, v and i, for “verbose” and “interactive” respectively, meaning it will tell us what is does and check with us if a file by the same name already exists. Try it now by putting -vi between cp and sample-1.txt in the previous command (you can answer “y” because we don't mind if your sample file is overwritten by the original sample file).
We can use the asterisk, which we call a wildcard, to copy multiple files. Enter this to copy all of your sample files to the test folder: cp -vi sample*.txt ~/testfolder1 and then list the files with ls.
We can use the copy files command to do even more complicated things that one might think would need two or three commands in sequence to achieve. Try this and see if you can figure out what it's going to do:
cp -vi sample-1.txt ~/testfolder2/sample-3.txt
ls /home/yourusername/testfolder2
Right, we copied sample-1.txt to testfolder2 but gave it the name sample-3.txt during the process of copying it.
How to rename and move files
But what if we just want to rename an existing file, rather than make a copy of it under a new name? It may seem odd to you but we use the command to move files to do that, instead of a command specifically for renaming.
When we move a file, we're free to rename it during the process if we want to, sort of like what we did when copying. So there's no reason why we can't move it under a new name to the very same directory it's already in.
Let's try it with sample-3.txt: cd to ~/testfolder2 and enter mv -vi sample-3.txt sample-4.txt and then list files to see the new name. Now move it back to testfolder1 with a new name by entering mv -vi sample-4.txt ~/testfolder2/sample-5.txt.
We've just been changing a single character in the file name so far, but we can rename files any way we like with this command. Once again, let's use sample-5.txt, that poor file that we've already chucked around all over the place. Enter mv -vi sample-5.txt new-sample-5.txt and then list the files and you will see the new name for the file.
Now let's move it to our other test folder. Since we aren't going to change its name during the move, we just enter mv -vi new-sample-5.txt ~/testfolder2, specifying only the target directory. Now let's move it back and give it yet another name by entering mv -vi ~/testfolder2/new-sample-5.txt ./old-sample-5.txt (remember that the single dot is shorthand for the current directory, so it represents ~/testfolder1 in this case). We can list files in both our test folders to see that it indeed is where we expect it to be and has the specified name.
We can also use wildcards in move operations, so enter mv -vi * ~/testfolder2 and then list the files in the current directory. Nothing will appear but we can look at testfolder2 and see that the files are indeed there now.
How to delete files and folders
Sometimes we need to delete files or even entire folders. For that we use the rm command, adding its -d option if our targets are directories instead of only files.
We've moved all of the files in testfolder1 to testfolder2 so let's delete testfolder1 now. Enter cd ~ and then ls to see the folders in your Home folder. Enter rm -vid testfolder1 to delete the folder and requiring you to confirm the deletion during the process. Now enter ls again and you will see that testfolder1 is gone.
You can use the man rm command in Terminal to learn more about it on your own, now that you understand the general concepts and syntax of working with files and folders on the command line. Be careful with rm because Terminal doesn't have an easy and obvious undelete command; you should probably use the options -vi to confirm each deletion. Generally, however, if you make a mistake that doesn't delete huge chunks of files from your system, you can use Nemo to browse the Trash (on the left column of bookmarks and favorite places) and find files to undelete there.
How to modify permissions of files and folders
In the last article, we learned about file and folder permissions. We used the file manager to change those permissions, using “friendly” terms and a point-and-click interface. We can use Terminal to modify permissions as well and sometimes it's much easier to do it that way. For one thing, if you read instructions for a particular purpose—for example, to make one or more directories or files on your website work correctly—and they say to set the permissions for files to 644, will you remember what that means to “translate” it to Nemo's option list of Read Only, Read and Write, or None? In fact, Nemo only offers a subset of possible choices, so the permission set you need might not be possible.
With the command line, we can do it easily. We use the chmod command. There are different styles of chmod so we could work with the r (read), w (write), and x (execute) permissions, setting them for owner, group, and others. Read the manual pages in Terminal to learn more about that but for now, let's do it the fast and easy way—using the 3-digit numeric code we discussed in the last article.
So, you will recall that 644 means Read+Write for owner, Read for group, and Read for others. If you are not there already, enter cd ~/testfolder2 to make it your current directory and then list the files in the detailed style with ls-l. Enter chmod -v 644 sample-1.txt. Terminal lets you know it didn't change anything because those were already the permissions in effect. Now let's try it again, but substitute 444 and Terminal will show you the result.
Start Nemo and navigate to testfolder2 in your Home folder. Double-click sample-1.txt and it should open in gedit. Notice that the window title tells you the file is read-only and you can't type or edit anything in the text area. Even though you are the owner, Linux is enforcing the permissions set for the file. Go ahead and close gedit.
Now let's set the permissions for sample-1.txt to 004 using chmod. Double-click it again in Nemo and gedit will tell you that you do not have permission to open the file.
Wut??? It's your file, you're the owner of it. Besides, it only makes sense that you should be able to open (read) it, right? Even if you, the owner, and you, as a member of the group, do not have permission to read the file, surely you should be able to do so as part of “everyone else” or “others.”
Except it doesn't work that way. It's a gotcha that occasionally stumps people but it does make sense.
Linux does the simplest possible check for permissions. Instead of running through all the possible combinations of your relationship to the file and the permissions set for it, it checks one relationship (owner), then if necessary, it checks another relationship (group). Then, based on those two relationship checks, it checks just one set of permissions:
- Is user the owner? If yes, check owner's permissions and stop.
- If no, is user a member of the group? If yes, check the group's permissions and stop.
- If no, check others' permissions and stop.
So, even if everyone else in the universe has permission to read the file, but you as the owner do not, Linux will simply stop after determining that you are the owner. It will not go on to check if you are a member of the group and what the group's permissions are. It's the most efficient process with the least amount of overhead, so even if it seems like a crazy gotcha, there's a method to its seeming madness.
Go ahead and chmod sample-1.txt back to 644. Now you should be able to open it with gedit again.
Modifying the permissions of a folder works exactly the same way, just specify the 3-digit code and the folder, including the path to it if it's not a child folder of your current directory.
The chmod command, along with many others, has a special option available: -R. That stands for recursive, which means that it will traverse all the subfolders, and all of their subfolders no matter how many levels deep, executing the chmod operation on all of the files or folders. The recursive ability makes these commands extraordinarily powerful, allowing a single typed command to act upon potentially thousands of files and folders. For this reason, you should always be very careful when using it; making a mistake with a single file can usually be fixed pretty easily but making that mistake with thousands could be a nightmare.
Imagine how long it might take you to use Nemo or another file manager to chmod files and folders, per instructions, in a typical website set up for a Content Management System (CMS)—such as Wordpress, Joomla, or Drupal—each of which contains several thousand files scattered among dozens and dozens of folders. A couple of commands using chmod (with the help of a couple of commands we haven't reviewed here) can take care of the permissions for all of those in two fell swoops (once for files and once for folders). There are other ways to do this in Linux but here are sample commands to change all the folder and file permissions in the current directory, including all of its subfolders:
- find . -type d -exec chmod 755 {} +
- find . -type f -exec chmod 644 {} +
The dot represents the current directory as the starting point to chmod the child folders and files. The first command sets folder (directory) permissions while the second sets file permissions. Use the Linux manual pages to understand what the commands do and note that you might need to further chmod a few specific files or folders afterwards, in line with instructions for your website's CMS.
The example above shows how understanding the command line can be extremely useful in some situations. For routine copying and renaming of files and folders, you will likely use a graphical file manager; I mostly use Nemo myself because it is indeed easy and convenient. But when faced with some tasks, the command line becomes an indispensable tool.
We've been experimenting in our own Home folder and its subfolders but now let's get a bit more daring. Let's copy sample-1.txt to the Home folder of our guest account: guestuser as we set up the account in the last article, but substitute whatever username you set up for your guests on your computer. Enter cp sample-1.txt /home/guestuser. Oh noes, we get an error message that permission is denied!
Enter ls -l /home and you will see that you are not the owner of the guest user's Home folder, nor a member of its group, and others do not have permission to write files in that folder (copying or moving is writing a file in the folder). Drat, foiled again by permissions, what to do?
Well, Tux and I will show you the solution to that problem in the next article, coming soon in the Tuxville blog.
This is the second half of my fifth article in a series about Linux. In previous articles, we took a quick tour of Linux Mint to get an overview, learned how to test drive it on our computers free of commitment or cost, went step by step through the process of installing Linux Mint, and learned about users, groups, and permissions. If you missed those articles, you can catch up by reading our group blog of Linux users, Tuxville. Visit the blog regularly to keep abreast of future articles in this series.
In a totally unrelated matter, I invite you to view another article I wrote a couple of days ago. It's a tribute to a great human rights activist who passed away recently. Her work was long ago, forgotten now by many who lived during that time, and unknown by younger generations. I think it's valuable for us, as liberals/progressives, to know about the contributions of those who preceded us; our past forms our present and can give us wisdom to create a better future.