Mastering the macOS Command Line: Part 4 — File Management

ElAmir Mansour
4 min readMay 4, 2024

Working with files and directories on the command line is fundamental to effectively navigating your computer’s file system and managing your data. Whether you’re a beginner or an experienced use.

understanding how to interact with files and directories using command-line tools is essential for tasks such as creating, copying, moving, renaming, and deleting files and directories.

In this introduction, we’ll explore the basic concepts and commands for working with files and directories in a Unix-like environment, such as macOS or Linux. We’ll cover essential commands like ls for listing files, cd for navigating directories, mkdir for creating directories, touch for creating files, cp for copying files, mv for moving or renaming files, and rm for removing files and directories. Additionally, we'll discuss concepts like relative and absolute paths, file permissions, and wildcard characters for pattern matching.

By mastering these fundamental concepts and commands, you’ll gain the confidence and skills needed to efficiently manage your files and directories from the command line, enabling you to perform a wide range of tasks with ease and precision. So let’s dive in and explore the world of files and directories on the command line!

Creating a File

Creating a file on the command line is straightforward using the touch command. Here's how you can create a new file named example.txt:

touch example.txt

This command will create an empty file named example.txt in the current directory. You can verify its creation by using the ls command to list the files in the directory.

Creating a Directory

To create a directory, you can use the mkdir command followed by the directory name. For example, to create a directory named my_directory, you would use:

mkdir my_directory

This command creates a new directory named my_directory in the current working directory.

Copying a File or Directory

To copy a file or directory, you can use the cp command followed by the source file or directory and the destination. For example, to copy a file named source.txt to a new file named destination.txt, you would use:

cp source.txt destination.txt

If you want to copy a directory and its contents recursively, you can use the -r option with cp. For example:

cp -r source_directory destination_directory

Avoiding Overwriting Files When Copying

By default, the cp command will overwrite an existing file with the same name as the destination file. To avoid overwriting files, you can use the -n or --no-clobber option. For example:

cp -n source.txt destination.txt

This command will prevent source.txt from being copied over destination.txt if destination.txt already exists.

Copying Multiple Files

To copy multiple files at once, you can specify multiple source files followed by the destination directory. For example:

cp file1.txt file2.txt file3.txt destination_directory

This command will copy file1.txt, file2.txt, and file3.txt to the destination_directory.

Copying a Directory

To copy a directory and its contents recursively, you can use the cp command with the -r option. For example:

cp -r source_directory destination_directory

This command will copy source_directory and all its contents to destination_directory.

Moving or Renaming a File or Directory

To move or rename a file or directory, you can use the mv command followed by the source and destination. For example, to move a file named oldfile.txt to newfile.txt, you would use:

mv oldfile.txt newfile.txt

Similarly, to move a directory named olddir to newdir, you would use:

mv olddir newdir

Avoiding Overwriting Files When Moving

Similar to copying, the mv command will overwrite an existing file with the same name as the destination file by default. To avoid overwriting files, you can use the -n or --no-clobber option. For example:

mv -n oldfile.txt newfile.txt

This command will prevent oldfile.txt from being moved over newfile.txt if newfile.txt already exists.

Moving and Renaming in One Go

You can also move and rename a file or directory in one go by specifying the new name and location. For example:

mv oldfile.txt new_directory/newfile.txt

This command will move oldfile.txt to new_directory and rename it to newfile.txt.

Moving Multiple Files

To move multiple files at once, you can specify multiple source files followed by the destination directory. For example:

mv file1.txt file2.txt file3.txt destination_directory

This command will move file1.txt, file2.txt, and file3.txt to the destination_directory.

Deleting a File

To delete a file, you can use the rm command followed by the file name. For example:

rm unwanted_file.txt

This command will permanently delete unwanted_file.txt. Be cautious when using rm as deleted files cannot be recovered.

Deleting a Directory

To delete a directory and its contents recursively, you can use the rm command with the -r option. For example:

rm -r unwanted_directory

This command will permanently delete unwanted_directory and all its contents. Use with caution as this action cannot be undone.

Using Symbolic Links

Symbolic links, also known as symlinks, are references to other files or directories. They act as shortcuts or aliases to the original files or directories. To create a symbolic link, you can use the ln command with the -s option. For example:

ln -s target_file symbolic_link

This command will create a symbolic link named symbolic_link that points to target_file. You can then use symbolic_link to access target_file.

Remember , practicing makes perfect , see you soon .

If you found this blog helpful or have any questions, feel free to reach out to me on social media:

--

--

ElAmir Mansour

🚀 Software Engineer & iOS Developer | Scrum Master 🕹 | Crafting Code & Content | Coffee enthusiast ☕️ | Simplifying Complexity, One Line at a Time 💻