A concise guide to understanding and utilizing the UNIX filesystem commands in an efficient manner.

Introduction

This guide provides a comprehensive overview of the UNIX filesystem commands, with a focus on macOS and GNU/Linux systems. Both of these systems are classified as UNIX, and they share many similarities in terms of their filesystem utilities. Although Windows has a different filesystem structure, users can still access and use similar utilities by installing the Windows Subsystem for Linux on Windows 10.

Understanding the Filesystem

Every computer system relies on a filesystem to store and retrieve data and programs. While memory is essential for storing data during active usage, a reliable and persistent storage system is necessary for preserving data when the computer is turned off or restarted. In the UNIX filesystem, the root node ("/") serves as the starting point, hosting the top-level directories.

Common directories found in UNIX systems include:

  • “/bin”: contains essential system commands
  • “/etc”: stores system configuration files
  • “/dev”: contains system devices
  • “/usr”: holds user files
  • “/tmp”: stores temporary files

The number of directories may vary depending on the system in use. Linux, for example, follows the Linux Standard Base and has standardized the directory structure. Some common directories found in Linux systems include:

  • “/boot”: contains files for booting the machine (not applicable to macOS)
  • “/home”: holds the home directories of users (equivalent to “/Users” in macOS)
  • “/opt”: used for storing user programs
  • “/usr/bin”: stores user binaries
  • “/usr/lib”: contains user libraries
  • “/tmp”: stores temporary files
  • “/var”: holds temporary files, logs, and more

macOS has its own set of directories, including:

  • “/Applications”: stores user applications
  • “/Library”: holds system-wide library settings and resources
  • “/System”: contains system files
  • “/private”: stores system files, logs, and more

Each directory contains files and subdirectories that can further contain additional files and directories.

File and Folder Naming

File and folder names have specific requirements in the UNIX filesystem. Names can be between 1 and 255 characters long and can include alphanumeric characters (A-Z, a-z, 0-9), underscores (_), periods (.), and commas (,). It is possible to use spaces in names, but it is generally not recommended as it requires escaping with a backslash whenever referencing the file or folder. Additionally, the filesystem may or may not be case-sensitive depending on the system. For example, macOS is not case-sensitive by default, so files or folders named “test” and “Test” would be considered the same.

Working with Folders and Files

mkdir

The “mkdir” command is used to create folders. For example, to create a folder named “fruits,” enter the following command:

mkdir fruits

Multiple folders can be created simultaneously by providing multiple folder names:

mkdir dogs cars

To create nested folders, use the “-p” option:

mkdir -p fruits/apples

cd

The “cd” command allows users to navigate into a specific folder. For example, to move into the “fruits” folder, use the following command:

cd fruits

To navigate to the parent folder, use “..”:

cd ..

To form a specific path, use a combination of folder names and special path indicators such as “.” (current folder) or “/” (root folder):

cd fruits
cd ../cars
cd /etc

pwd

The “pwd” command displays the current folder’s path:

pwd

rmdir

The “rmdir” command is used to remove empty folders. For example, to delete the “fruits” folder, use the following command:

rmdir fruits

Multiple folders can be deleted at once:

rmdir fruits cars

For folders with files, the more general “rm” command with the “-rf” options is used for deletion. However, be careful when using this command, as it does not prompt for confirmation and will immediately delete the specified files or folders:

rm -rf fruits cars

ls

The “ls” command lists the files and folders within a given directory. To list the contents of the current directory, use the following command:

ls

To list the contents of a specific directory, provide the directory path after the “ls” command:

ls /bin

The “ls” command accepts various options to modify its behavior. One common option combination is “-al”, which provides detailed information, including permissions, file size, and modification date:

ls -al /bin

touch

The “touch” command is used to create an empty file. For example, to create a file named “apple”, use the following command:

touch apple

If the specified file already exists, “touch” updates the file’s timestamp.

mv

The “mv” command is used to move or rename files. To move a file from its current location to a new location, specify the file’s current path followed by its new path:

mv test pear

To rename a file, use the “mv” command in the same way but with the new desired name:

mv pear new_pear

For multiple files, specify all the file names followed by the destination folder:

mv pear apple fruits

cp

The “cp” command is used to copy files. For example, to copy a file named “apple” to a new file named “another_apple”, use the following command:

cp apple another_apple

To copy directories, use the “-r” option to recursively copy the entire folder contents:

cp -r fruits cars

ln

The concept of links allows files to point to other files. There are two types of links: hard links and symbolic (soft) links. For practical purposes, symbolic links are more commonly used. To create a symbolic link, use the following syntax: “ln -s original linkname”. For example, to create a symbolic link named “newfruits” pointing to the “fruits” folder, use the following command:

ln -s fruits newfruits

Permissions

File and folder permissions can be modified using the “chmod” command. There are two ways to use “chmod”: symbolic arguments and numeric arguments.

Symbolic arguments use letters to represent different access levels (“a” for all, “u” for user, “g” for group, “o” for others) and “+” or “-” to add or remove permissions. For example, to allow everyone to read a file, use the following command:

chmod a+r filename

To allow everyone to read and write a file, use the following command:

chmod a+rw filename

To restrict others (not the owner or in the same group) from reading, writing, or executing a file, use the following command:

chmod o-rwx filename

Multiple permission levels can be modified simultaneously by combining letters before “+” or “-”:

chmod og-r filename

Numeric arguments are faster but may be harder to remember. Numeric values represent different permission levels (maximum value is 7):

  • 1: execute permission
  • 2: write permission
  • 4: read permission

Using these values, permission combinations can be set:

chmod 777 filename
chmod 755 filename
chmod 644 filename

The “chown” command is used to change the owner of a file:

chown <username> <filename>

The “chgrp” command is used to change the group associated with a file:

chgrp <group> <filename>

Managing File Content

less

The “less” command is a powerful tool for viewing file contents. It provides an interactive interface for easy navigation. To view the contents of a file, use the following command:

less filename

Once inside the “less” session, you can navigate using the arrow keys, spacebar, and other keys. To quit, press “q”. You can search for specific words within the file by pressing “/” followed by the search term. Use “n” to jump to the next occurrence and “N” to jump to the previous occurrence.

tail

The “tail” command is useful for viewing the end of a file, especially log files. When used with the “-f” option, it continuously updates the file display as new content is added. For example, to watch the system log file in real-time, use the following command:

tail -f /var/log/system.log

To exit, press “CTRL-C”. The “tail” command can also display a specific number of lines or start from a specific line.

cat

The “cat” command can be used to display file contents or combine multiple files. For example, to display the contents of a file, use the following command:

cat file

To display the contents of multiple files, provide their names after the “cat” command:

cat file1 file2

To combine the contents of multiple files into a new file, use the output redirection operator (">"):

cat file1 file2 > file3

To append the contents of multiple files to an existing file, use the append operator ("»"):

cat file1 file2 >> file3

The “cat” command can also display line numbers using the “-n” option and can be combined with the pipe operator ("|") to feed output to another command.

wc

The “wc” command stands for “word count” and provides information about the number of lines, words, characters, or bytes in a file. Use the following commands to count specific metrics:

  • To count lines: “wc -l filename”
  • To count words: “wc -w filename”
  • To count characters: “wc -c filename”
  • To count characters with multibyte support: “wc -m filename”

“wc” can also be used to count multiple files, providing a summary of the counts.

find

The “find” command allows users to search for files or directories based on specific criteria. By using various options, users can search recursively and customize the search pattern. Here are some examples:

  • To find all files with the “.js” extension under the current tree: “find . -name ‘*.js’”
  • To find directories with the name “src” under the current tree: “find . -type d -name src”
  • To exclude a specific path from the search: “find . -type d -name ‘.md’ -not -path ’node_modules/’”
  • To find files larger than a specific size: “find . -type f -size +100k”
  • To find files modified within the last 24 hours: “find . -type f -mtime -1”

The “find” command can also perform actions on the search results, such as executing a command on each file or deleting files.

Conclusion

Understanding and mastering the UNIX filesystem commands can greatly enhance your ability to navigate, manipulate, and manage files and folders. By utilizing these commands efficiently, you can streamline your workflow and optimize your productivity.