PATH: Instructional Server> CTS 2106> Exercises>

CTS 2106 - Exercise I - Managing Files and Directories


Chapter I of our Virtual Textbook introduces the issues and techniques used to manipulate files on a Linux system. Understand that Linux is designed to treat all objects that provide or receive data, including devices such as keyboards, monitors, disk drives, etc., as files. As such, commands which manipulate files are very versatile. Also be aware that directories (also known as folders) are just another type of file; one which holds data allowing access to other files. Root users and ordinary users spend much of their time on a Linux system creating and manipulating files. The exercise below will show commonly used commands related to file management. Novice users should review the information about command syntax in Exercise D before performing this exercise.


All of the steps below should work in any Linux distribution. So the first step is to access the character-based shell of any Linux distribution. If you have access to a Linux system, just login and open a terminal window. If your Linux system boots to a graphic desktop and you do not see an icon on it named Terminal, then open a "virtual terminal" by pressing the keystroke combination Ctrl+Alt+F2 (the Function 2 key). If you have not yet used or installed Linux, you can access a copy of it (installed on the IRSC student server) remotely from a Windows PC by following the directions on the page entitled Instructions for Remote Access to the IRSC Linux Server. The remote access method logs you into a character-based terminal window automatically. Once you have logged into a terminal window, try the commands in the sections below.

Knowing Your Environment

In Exercise G, readers learned how to examine their working environment. Before working on files, it is wise to know a few important facts: your present working directory, your path, and your user mask. The commands below can be used to determine these.

Each filing command executed on a Linux system should indicate where in the file system (hierarchy) the command should take place. This can be done explicitly by providing path information within the argument of the command, or can be determined by default based on the values of some system environment variables, such as PATH, HOME, and PWD.

Present Working Directory

The environment variable PWD keeps track of which single directory a user is focused on while working. Unless otherwise indicated, each filing command executed on a Linux system will affect or use the directory indicated by this pointer value. Try both commands below to view this value on your system for your session:

If your present working directory is not the home directory of your user account, then change to that directory by entering the command: cd

Search Path

The environment variable PATH contains a colon delimited list of directories searched by the system whenever a user executes an external command without explicitly indicating which directory contains the executable file. The directories are searched in the order listed in the string found in the variable. Try both commands below to view the path value on your system for your session:

User Mask

Each newly created file (or directory) on a Linux system is assigned a set of [file system permissions] for three classes of user: the owner, the owner group, and everyone else. Students should review the writings about both the symbolic and numeric (octal) [permission notations] used to indicate the [mode] of each file. Be aware that most systems report the mode as 3 octal digits, with each position indicating a class permission status. Some commands show modes as 4 digits, in which case the extra (leftmost) digit is used to indicate special permissions rather than a class position. These are rarely used, so they often appear as the digit 0 (zero). On most systems, the initial permissions assigned to a new ordinary (non-executable) file is octal 0666, allowing read/write access to all users. The initial permissions (mode) assigned to a new executable file or directory is octal 0777, allowing read/write/execute access to all users. In most cases, these modes are too permissive and should be limited somewhat. This is accomplished automatically by Linux each time a new file is created by applying an octal number called a "user mask" to the initial mode to establish a less permissive effective mode, which becomes the final permission assigned to the file. The user mask value is set within a startup script and can be viewed or altered using the umask command. When entered without argument, the command will display the current user mask value. When executed with a proper octal argument, the user mask value to be applied to limit modes on future files will be set to the value indicated. For example, try the commands below to show and then revise the user mask for the system:


There are a wide variety of techiques to create files. Many programs and utilities create files by simply saving data (in a variety of data languages) to a storage device. Files can also be created in Linux by redirecting output using the '>' metacharacter. For example, you can redirect the output of an ls command into a file (perhaps named "outfile") with a command such as: ls -l /etc > outfile

The simplest form of file creation is the creation of an empty data file which can be accomplished with either of these equivalent commands:

Directories (folders) are most often created using the mkdir command. Try making a directory named "mydir" inside the current directory using the command: mkdir mydir


All operating systems have a file copying command (often using the keyword "copy"). The file copying command in Linux is a shorter form, cp. Try making a copy of the previously created file "emptyfile" to a new file named "emptyfile2" using the command: cp emptyfile emptyfile2

Notice the order of the arguments: source filename first, destination filename second. The second argument can also be the name of a directory into which you want the source file copied (using the same file name), as in: cp emptyfile mydir
Verify that the copy of the file is in the directory mydir using the command: ls -l mydir

Relocation (and Renaming)

All operating systems also have a file moving command to relocate a file to a different directory. The file moving command in Linux is mv. Try moving the previously created file "emptyfile2" to the folder "mydir" using the command: mv emptyfile2 mydir

The mv command does not actually physically move the file to a new location. Instead it simply moves its directory entry to a new location. Because the mv command does its job by manipulating directory entries, it can also be used to rename a file. For example, to rename the file named "emptyfile" to be "blankfile", use the command: mv emptyfile blankfile

Verify the filename change in the current directory with the command: ls -l


Eventually, files and directories must be removed from the file system. The basic Linux command to remove a directory is rmdir. A directory must be empty before it is removed. Try to remove the (unempty) directory "mydir" with the command: rmdir mydir

You will get an error message refusing to comply. The necessary step is to remove the files first. The Linux command to remove one or more files is rm. Try to remove the all files that might be in directory "mydir" with the command: rm mydir/*

Now, remove the empty directory "mydir" with the command: rmdir mydir

Verify the removal from the current directory with the command: ls -l


For more examples of the commands used to manipulate files and directories, review the reading in Chapter I of our Virtual Textbook.

PATH: Instructional Server> CTS 2106> Exercises>