knowledge-kitchen

Unix Commands - Navigating and Managing the File System

for those who don’t know

  1. Overview
  2. Assumptions
  3. Navigating with *NIX
  4. Managing files
  5. Permissions
  6. Running Programs
  7. Conclusions

Overview

Concept

The UNIX/Linux “command shell” (sometimes called the “command line”) is a text-based interface between a user and the operating system of a computer.

Support

Almost all contemporary personal computers support some variation of the UNIX/Linux command shell based on bash.

UNIX or Linux

The histories of UNIX and Linux are heavily intertwined, and so we most often refer to them as a single type of operating system, even though there are many variations of each.

Assumptions

Assumptions

Ability to run *NIX commands

In order to run Unix or Linux commands, you will need a terminal emulator:

Navigating with *NIX

Overview

Unix commands are necessary in order to be able to navigate directories/folders in a UNIX-like system.

Review basic Unix commands.

Where am I?

At any point in time, you are actively within one specific directory, known as the current working directory. To determine which directory that is at any point, run the pwd command:

foo@bar$ pwd
/Users/foo

It’s fine to read about this, but you’ll learn better if you try it out yourself.

Read more about the current working directory.

How do I go up one level

To navigate up one level in the directory structure of your hard drive, use the .. shortcut no matter where you currently are.

foo@bar$ pwd
/Users/foo
foo@bar$ cd ..
foo@bar$ pwd
/Users
foo@bar$ cd ..
foo@bar$ pwd
/

We have now navigated to the root directory, represented by the symbol /.

How do I get to the top?

To navigate to the top-most directory of your hard drive, known as the “root directory”, use the cd command with the / symbol indicating root directory as the desired destination.

foo@bar$ cd /
foo@bar$ pwd
/

The current working directory is now the root directory.

How do I see what directories and files are within the current working directory?

Use the ls command to show non-hidden files and directories that are within the current working directory.

foo@bar$ ls
dir1    dir4    file2
dir2    dir5
dir3    file1

This listing contains a mix of files and directories - there is no way to tell which is which simply based on their names. In order to distinguish between them, we need some metadata on each item in this listing.

How do I see what hidden directories and files are within the current working directory?

Hidden files and directoriess simply begin with the ‘.’ character. To view a listing of files and directories including them, use the a flag on the ls command.

foo@bar$ ls -a
.hidden_dir1    dir1    dir4    file2
.hidden_dir2    dir2    dir5
.hidden_file1   dir3    file1

Both files and directories may be hidden by naming them prefixed with a period,..

File listing with metadata

To view a listing of files and directories including metadata about each item, use the l flag on the ls command.

foo@bar$ ls -a
drwx------+ 10 foo  bar   320 Sep  4 10:41 dir1
drwx------+ 19 foo  bar   608 Aug 30 21:38 dir2
drwx------+  8 foo  bar   256 Nov 27  2017 dir3
drwx------+  4 foo  bar   128 Dec 31  2016 dir4
drwx------+ 19 foo  bar   611 Aug 21 21:38 dir5
-rwx------+ 10 foo  bar   320 Sep  4 10:41 file1
-rwx------+ 19 foo  bar   608 Aug 30 21:38 file2

To include hidden files in the listing, combine the -l and -a flags.

foo@bar$ ls -la
...

What does all that metadata mean?

foo@bar$ ls -a
drwx------+ 10 foo  bar   320 Sep  4 10:41 dir1
drwx------+ 19 foo  bar   608 Aug 30 21:38 dir2
drwx------+  8 foo  bar   256 Nov 27  2017 dir3
drwx------+  4 foo  bar   128 Dec 31  2016 dir4
drwx------+ 19 foo  bar   611 Aug 21 21:38 dir5
-rwx------+ 10 foo  bar   320 Sep  4 10:41 file1
-rwx------+ 19 foo  bar   608 Aug 30 21:38 file2

The metadata included in a long listing of files and directories in the working directory indicates a few different things, including:

How do I drill down?

To navigate to sub-directory that is within the current directory, use the cd command with the name of the sub-directory that is the desired destination.

foo@bar$ cd bar
foo@bar$ pwd
/bar

Note that writing cd /bar, with the forward slash, would navigate to a directory named bar that is a sub-directory of the root directory, if any such directory exists, which is not what you wanted.

You can, of course drill down several levels of directories at once.

foo@bar$ cd bar/baz/blue
foo@bar$ pwd
/bar/baz/blue

How do I jump to a directory from anywhere

If you kmow the full path of a directory to which you want to navigate, type it after the cd command.

foo@bar$ pwd
/bar/baz/blue
foo@bar$ cd /Users/foo/Documents/my_favorite_ice_cream
foo@bar$ pwd
/Users/foo/Documents/my_favorite_ice_cream

Managing files and folders

Create a blank new file

The touch command is useful for creating a blank file with a given name.

foo@bar$ touch environmental_cosmetology.txt

To open up this new file in the notorious emacs command-line text editor:

foo@bar$ emacs environmental_cosmetology.txt

Rename or move a file

Renaming is a variety of moving, using the mv command to move a file from one name to another.

foo@bar$ mv environmental_cosmetology.txt ecdp.txt

Or keep its current name, but move it to an entirely different directory:

foo@bar$ mv environmental_cosmetology.txt /Users/foo/Documents/

Or rename it and move it to an entirely different directory up two levels and down from there into a subdirectory named baz:

foo@bar$ mv /Users/foo/Documents/environmental_cosmetology.txt ../../baz/ecdp.txt

Delete a file

The rm command deletes files.

Delete a file in the current working directory:

foo@bar$ rm heme_rich_foods.csv

Delete a file in a sub-directory of the current working directory:

foo@bar$ rm meat/fillers/heme_rich_foods.csv

Delete a file in a subdirectory of the root directory:

foo@bar$ rm /meat/fillers/heme_rich_foods.csv

Delete a directory

The rmdir command deletes empty directories, while the rm command can be used to delete non-empty directories.

Delete an empty sub-directory named meat:

foo@bar$ rmdir meat

Delete a non-empty directory with the r and f flags of the rm command:

foo@bar$ rm -rf meat

Create a directory

The mkdir command creates an empty directory.

Make a new sub-directory named vegetables:

foo@bar$ mkdir vegetables

Make a sub-directory of the parent directory:

foo@bar$ mkdir ../arugula

Make a new directory somewhere totally different on the hard drive:

foo@bar$ mkdir /Users/foo/Photos/compromising/nudes

Create multiple directories

The mkdir command by default will only create one directory at a time.

The -p flag allows it to create a series of directories and sub-directories in one command, e.g.:

foo@bar$ mkdir -p vegetables/arugula/baby

Permissions

Users

It is possible to control who can access any file or folders.

Users who have access to a file or folder fall into one of three categories:

Access types

There are three types of access to a file or foldeer:

Example

-rwxr-xr--+ 10 foo  bar   320 Sep  4 10:41 file1.sh

Given the file metadata above, we can see the following permission settings:

Revoke permissions

Revoke permissions with the - sign. In this case we revoke the user foo’s execute permission:

foo@bar$ chmod u-x file1.sh

Grant permissions

Grant permissions with the + sign. In this case we grant the group bar write permission:

foo@bar$ chmod g+w file1.sh

Others

All other users besides the user and group who own the file are known as ‘others’ and can have permissions granted or revoked as well:

foo@bar$ chmod o+wx file1.sh

Executing Programs

Java

Directory structure

A well-organized Java project might consist of a project directory with at least 3 sub-directories:

project-directory/
       |
       |----------> src/
       |
       |----------> lib/
       |
       |----------> bin/

Compiling

Before a Java project can be run, any Java source code in the src/ directory must be compiled into Java byte code in the bin/ directory.

Assuming the current working directory is the project directory:

foo@bar$ javac -d bin src/foo/bar/SomeFile.java

Executing

To run the Java project, you can execute the Java byte code file in the bin/ directory.

Assuming the current working directory is the project directory:

foo@bar$ java -cp bin foo.bar.SomeFile

Conclusions