About the Unix Command Line#
Introduction to the Unix Command Line#
The Unix command line is a powerful tool that allows you to interact with your computer's operating system through text-based commands. It is widely used in various platforms, including Linux and macOS. Mastering the command line can greatly enhance your productivity and enable you to perform various tasks efficiently.
In this tutorial, we will cover the basics of the Unix command line, including navigating the file system, manipulating files and directories, and some useful commands.
Getting Started#
Opening the Terminal#
To access the Unix command line, you need to open the terminal or shell application on your computer. On Linux systems, you can use the built-in terminal emulator. On macOS, you can use the Terminal application, which can be found in the Utilities folder within the Applications folder.
Basic Commands#
Let's start with some fundamental commands to get you acquainted with the Unix command line:
-
pwd
(Print Working Directory): Displays the current directory (folder) you are in.pwd /Users/username/documents
-
ls
(List): Lists the files and directories in the current directory.ls file1.txt file2.txt folder1 folder2
-
cd
(Change Directory): Allows you to navigate to a different directory.cd folder1
-
mkdir
(Make Directory): Creates a new directory.mkdir new_folder
-
touch
: Creates an empty file.touch new_file.txt
-
rm
(Remove): Deletes files or directories.rm file1.txt rm -r folder1
-
mv
(Move): Moves or renames files and directories.mv file2.txt folder2/ mv file2.txt new_filename.txt
-
cp
(Copy): Copies files or directories.cp file1.txt folder2/
Navigating the File System#
The Unix file system is organized in a hierarchical structure. Each directory can contain files and other directories. Here are some additional commands to help you navigate the file system:
-
cd
(Change Directory): As mentioned earlier,cd
allows you to change your current directory. You can use absolute or relative paths.cd /path/to/directory # Absolute path cd ../parent_directory # Relative path (move up one level) cd folder3/subfolder # Relative path (move down into subfolder)
-
ls
(List): You can use various options withls
to customize the output:ls -l # Long listing format with detailed information ls -a # List all files, including hidden ones (those starting with .) ls -h # Human-readable file sizes ls *.txt # List all files ending with .txt
-
pwd
(Print Working Directory): Useful to check where you are in the file system. -
..
and.
:..
refers to the parent directory, while.
refers to the current directory. They can be handy when constructing paths.
File Manipulation#
Now let's explore some additional commands to manipulate files:
-
cat
(Concatenate): Displays the content of a file.cat file1.txt
-
less
andmore
: Allow you to view the content of large files interactively.Use the arrow keys to scroll, and pressless large_file.txt more another_large_file.txt
q
to exit. -
head
andtail
: Display the beginning or end of a file.head file1.txt # Show the first few lines tail file1.txt # Show the last few lines
-
grep
: Searches for a pattern in a file.grep "keyword" file1.txt
-
wc
(Word Count): Counts the number of lines, words, and characters in a file.wc file1.txt