Linuxfscommands

Linux File System Commands #

Due to a component shortage we will be starting Linux later in the year
Unfortunately there is a worldwide shortage of Raspberry Pis at the moment and until we can source parts we will have to delay the Raspberry Pi/Linux portion of the class. You can skip this section for now.

The File System #

The manner in which Linux stores and interacts with files is different from Windows with which you might be more familiar.

On Linux, the file system is made up of directories. A directory can contain a list of files or directories. The directories can then in turn contain a list of files or directories.

The term folder and directory are interchangeable, but Linux commands and manuals tend to use the term directory whereas Windows and Mac use the term folders.

On Linux, all files and folders are case sensitive.

Linux uses forward slashes / to denote directories, Windows uses back slashes \. There is no concept of the C:\ drive on Linux, all of the files are under the root directory /.

Command Line Arguments #

Often when typing a command on Linux we will need to specify some additional information such as a filename or folder. Arguments such as these are typed directly after the command itself, separated by a space.

In the following example, we use the nano command and give it the filename we want to open as an argument file1.c.

pi@raspberry:~ $ nano file1.c

Different programs use different conventions and we will see a mix of different styles below.

Everything is a file! #

On Linux, everything is treated as a file.

Some of the documentation will refer to files, and that can include directories too.

The File System #

There is a suite of related programs that you can run from the command line that allow you to navigate and manage the file system.

The prompt itself contains information about the current directory that you are working in:

pi@raspberry:~ $

The ~ above is short hand for the home directory.

All users on Linux systems have a home directory. Home directories are stored in the /home folder. The username we are using on the Pi is pi, and the machine name is raspberry. You can see all of that information in the prompt above.

Working Directory #

When you open a new terminal, you are automatically put in the home directory.

pi@raspberry:~ $

Again, the ~ (or tilde) is short hand for the home directory.

To print the working directory use the command: pwd

pwd Print Working Directory #

The pwd command simply prints the working directory, the directory in which you are currently situated. Any commands we issue will default to this directory.

pi@raspberry:~ $ pwd
/home/pi

The /home/pi directory is the home directory for the user pi. In most command line situations, you can type the ~ in place of typing /home/pi

touch Creating a new empty file #

The touch command is used to create empty files, and to update the last modified time of files.

pi@raspberry:~ $ touch file1.c

This will create a new empty file called file1.c.

Keep in mind that the file names are case sensitive!

ls List files in directory #

The ls command is used to list the contents of a directory. The ls command can be used with the following flags:

  • -a will show all of the files, including hidden files
  • -l (that’s a lowercase L) will list the files with a long listing format including more information
pi@raspberry:~ $ ls –l

By default, ls will operate on the current working directory, but we can also specify a directory as a command line arguement:

pi@raspberry:~ $ ls -l dir1

The above will list the contents of a folder called dir1 that is directly inside of the home directory. We can also specify an absolute directory path by beginning with the the forward slash:

pi@raspberry:~ $ ls -l /home/pi/dir1

mkdir Create a directory #

The mkdir command makes directories. Simply give the name of a directory and it will be created in the current working directory.

pi@raspberry:~ $ mkdir labs

The above command will create a new folder called labs in the current working directory. We can see from the prompt that the home folder is the current working directory, that will result in the full path of the folder being: /home/pi/labs

cd Change Working Directory #

The cd command changes the current working directory.

pi@raspberry:~ $ cd labs
pi@raspberry:~/labs $

The above command changes the working directory to the labs folder, you can see the prompt has changed so that we can see the current working directory is labs.

To move up one directory, we can use .. as shorthand for the parent directory.

pi@raspberry:~/labs $ cd ..
pi@raspberry:~ $

The current directory is denoted with a single ., we have used this before when running our compiled C program:

pi@raspberry:~ $ ./a.out
Hello, World!

rm Remove a file or directory #

The rm command will remove a file or directory.

pi@raspberry:~ $ rm file1.c

This will delete the file1.c file.

The rm command won’t let you delete a directory by default to protect you against accidental deletion. To delete an already empty folder you can use the switch -d and to force a delete of a folder and all of its contents you can use -r, but that is very dangerous as it does not ask you if you would like to delete the files, it just does it.

man Read the Manual! #

More information on all of these commands can be found in the manual. The man command can be run on most of the commands on the system. To find out more about a command, like rm for example, simply type:

pi@raspberry:~ $ man rm

To exit from the manual, press q on the keyboard.