Don't Need a Weatherman to Know Which Way the Wind Blows
Last time out, we dealt with the mindset and resources needed to succeed in UNIX. Primarily, a sense of ownership and a drive to solve problems. In this installment we are going to look at a couple of commands that are the core of keeping a usable system. Commands for directory manipulation and variables. If you get stuck or are just feeling froggy dump them into the triumvirate (help, man, info) and read up on some of the more obscure uses. Hope you got your coffee and notebook, Let's get to some serious fiddling.
Homeward Bound:
To find out where we are with in the system, we are going to print the working directory (pwd) this will provide the path we are currently executing commands from and creating files in.termux@amelia -: pwd /data/data/com.termux/files/hometo see what directories and files are located in this directory, list (ls) them.
termux@amelia -: ls projects storage
If you are using a modern system, this is likely going to show up with different colors. We will get to customizing those in a bit. For now, just know that the different colors show different file types. There are a few nifty options that ls provides. Check out the help systems for things like -a -F -d -l. All very cool things, depending on what you are needing and the capabilities of the console. Back when I was first learning this stuff I was using amber and green screens, so the colors of the modern console were useless, but -F was extremely useful.
The directories in your current directory will likely be significantly different than what is displayed for me. So let's make sure we are in the same place. The default starting place. The home directory. When creating new users, the system will set up a home directory. This space is carved out for you and you alone. You have free reign to do any crazy thing here as long as it fits under the quota and permissions. You can create files; build webpages, collections, or applications, rename stuff, change extensions, your wish is bash's command.
termux@amelia -: cd
Since we did not supply a destination directory for cd, it figured we wanted to go home. Let's show the directory that we are in again (pwd, look up there way at the top). It may print out the same as it did before or something else. It will likely end in either your username or home. Just to double check, let's display where we are and what our home directory.
termux@amelia -: echo $PWD $HOME /data/data/com.termux/files/home /data/data/com.termux/files/homeThat is a lot of characters and a little hard to read. Let's let the shell verify it for us
termux@amelia -: test $PWD = $HOME; echo $? 0
In these listings, we are using a couple new commands that come in real handy. echo prints whatever is after it (after filling in variables (that is what the leading $ means) and other forms of expansion. test tells us if something is true or false (0 is true/successful execution, any other number is the exit code for the failed execution). And last, but not least ; is a command separator that allows you to put multiple commands on the same line. We see a couple of variables mixed in. You can make a variable out of just about any word, except for the special words the shell really likes. You simply set it with an assignment operator(=). Note there are no spaces areound the operator. If you do add spaces you will likely see a 'command not found' error. Wonder what that exit code would be. . . .
termux@Thu, 2020-11-26 08:37 -: test_variable='This is a test' termux@Thu, 2020-11-26 08:45 -: echo test_variable test_variable termux@Thu, 2020-11-26 08:45 -: echo $test_variable This is a test
Note that you do not use the $ when you assign a value to a variable; but, if you leave it off when you echo the variable, it prints out exactly what you typed, the variable name. The variables in all capital leters are special these are used to specify things that the shell uses to make decisions or configurations. We call these environmental variables.
- $PWD
- The present working directory. This should print out the same thing as if we typed pwd.
- $HOME
- The path to our home directory
- $?
- The exit status of the last command run by the shell. A 0 equals a success
After getting comfortable printing out some variables and whatnot, you are ready to create a new directory to hold the random stuff that we create and muck about with. bin, src, scripts, sh are all commonly used by folks. I have known more than a few that have used foo, foo1, foo2, . . . A lot of the modern systems will set up Desktop, Documents, Downloads, and so on, but as i am looking to do most of my work in the shell instead of clicking, spaces and capital letters get annoying so I use something that looks like this:
- projects
- Anything that will be a long term time/space sink. Common subdirectories include research, scripts, bin, website This is where most of my git repositories will hang out (we will get to git in a bit).
- docs
- General document repository. Common subdirectories include bills, personal, guides, tickler, a-f, g-k, l-r, s-z. The last sets are for filing longterm stuff that isn't important enough currently to take up mental space, but might come in handy later on down the road. The 'tickler' directory thing is for things that I want to check on every week or so (comes from the 'Getting Things Done' (GTD) methodology)
- downloads
- Things I got from the intertubes, what, what! Most common directories here are going to be podcasts, images, keys. The 'images' here is for images that contain installation media (CD/DVD/USB) and keys contains digital signatures to make sure that media is not corrupt/compromised
- pics
music
movies - Pretty self-explanatory
termux@Thu, 2020-11-26 08:37 ~: mkdir projectsNow, when you list the directory (ls), you should see projects appear. To change to this new directory:
termux@Thu, 2020-11-26 08:37 -: cd projects
Absolutely Relative
Print the working directory again. It should now end in 'projects' if everything went according to plan. There is likely a good bit of stuff sparated by / in front of it. This is known as the absolute path. It allows you to explicitly declare where on the system something is located. When we changed into the directory 'projects' we could have typed that whole thing out. but instead the shell lets us shortcut this method by only putting in the last part, because it is located in the same directory we were. To test this our, let run a little test.
termux@amelia ~: pwd /data/data/com.termux/files/home/projects termux@amelia -: my_wd=$PWD termux@amelia -: cd .. termux@amelia -: pwd /data/data/com.termux/files/home termux@amelia -: echo $my_wd $PWD /data/data/com.termux/files/home/projects /data/data/com.termux/files/home termux@amelia -: cd $PWD termux@amelia -: pwd /data/data/com.termux/files/home termux@amelia -: cd $my_wd termux@amelia -: pwd /data/data/com.termux/files/home/projects termux@amelia -: unset my_wd
This one is a little long, but covers a lot of ground. First it prints what directory we are in. Then we assign the present directory ($PWD) to a variable ($my_wd). Next we change to the parent directory (..). This a new one for us. On UNIX system any file that starts with a . is considered 'hidden' and will not be printed using when we aske what is in the current directory. Usually these files are used for cache, configuration files, history files. Things that are needed for the system to operate, but that don't get touched very often and would tend to clutter up the eye-line of the directory structue. If you pass the -a flag to ls, it will show all these directories and files. In this exercise, we care primarily about 2 of these . and .. These represent the current and the parent directory. When you type cd . (there is a space between cd and .) it tells the shell to change to the directory you are in (the same as we did above by cd $PWD. This will come in handy when we start exploring PATH and command execution. cd .. tells the shell to take you to the directory that contains this one. Since each directory contains both of these (. and ..) it is theoretically possible to do this for infinity (or until the shell tells you to knock it off). Once we are in the parent directory, we print the working directory again and validate that $my_wd no longer matches $PWD. Then we do the side to side shuffle and change to the directory we are already in (cd $PWD) and prove it did nothing by printing out the directory again. then we change back to projects by calling the absolute path from $my_wd and verify where we are again. Finally, we unset our variable. This will hapen automatically when we leave the shell, but it is a good habit to get into to clean up after yourself; for those times you leave sessions running for years on end.
The last one we are going to touch on this go-round is how to get rid of directories. This is the opposite of making directories and is named . . . rmdir. !!!WARNING!!! Removing files and directories from the commandline in UNIX systems is mostly destructive Make sure you have backups if there is any possibility that you will need access to the data. We will get to backing up and restoring stuff a little down the road. What we are doing right now. is just within our projects directory and using temp directories so nothing is really at stake here. In the projects folder, we are going to make a new directory.
termux@amelia -: mkdir -p testing_folder/holder
When you check the current directory you will only see testing_folder showing, so what happened to holder? The -p flag to mkdir means that we want to create all the folders in the path if they don't already exist. If you change to the testing_folder and check the directory listing again you will see holder is safe and sound and waiting to be populated with fantastical data. Change back to the parent directory (cd ..) and try to remove the testing_folder. You should see an error that looks something like the following:
termux@amelia -: rmdir testing_folder rmdir: failed to remove 'testing_folder': Directory not empty
Holder is in there preventing us from getting rid of our testing_folder. We can either change into the testing_folder ond remove the directory from there or we can do it all in one swell foop. That -p flag we sent to mkdir means and your parents, too, to rmdir. This will only work if all folders in the path are empty; otherwise, you will see the 'Directory not empty' statement for any folders in the path that have other files or folders.
This should be a good bit to chew on. Make some directories, make some variables, make your shell say rude things. In the run up to the next article, I strongly urge you to run through vimtutor. Next time we will look at creating, modifying, and removing files.
73s and hug your knees, ArloUseful commands:
- cd
- test
- mkdir
- ls
- echo
- rmdir
Environment variables:
- HOME
- PWD
Comments
Post a Comment