Basic Unix Commands#

cd (Change Directory):#

Description:

The cd command is used to navigate from a directory to another directory.

Example:

cd /home/user/documents - Changes the current directory to /home/user/documents.

cd ~ - Brings you to your home directory.


ls (List):#

Description:

The ls command is used to list the files and directories in the current directory or a specified directory. It provides information about the contents of the directory, such as filenames, permissions, sizes, and timestamps.

Example:

ls - Lists the files and directories in the current directory.

ls -all /home/user/documents - Lists the contents of the /home/user/documents directory showing detailed information about each file and directory.

There are other flags that can be used with ls to list the files/directories in different ways e.g.,

ls -l provides more information such as the permissions settings, the file size, the time of creation etc.

ls -ltr lists the files in reverse order of the time they were created. This can be useful when your model run crashes and you want to see which log file was most recently created.


pwd (Print Working Directory):#

Description:

The pwd command displays the name of the current working directory, which is the directory in which you are currently located. It helps you determine your current location in the directory tree.

Example:

pwd - Displays the location of the current working directory, such as /home/user.


cp (Copy):#

Description:

The cp command copies files or directories from a source location to a destination location.

Example:

cp file1 file2 - Copies the contents of file1 to file2

cp -r directory1 directory2 - Copies the contents of directory1 to directory2. The -r here is needed to recursively copy all the contents of the directory1 to directory2


rm (Remove):#

Description:

The rm command deletes files or directories. Be cautious, as the operation is irreversible.

Example:

rm file1 - Deletes a file named file1

rm -rf directory1 - Removes a directory named directory1 and all of its contents.


mv (Move):#

Description:

The mv command moves or renames files and directories. It can also be used to move files from one directory to another.

Example:

mv file1 file2 - Renames file1 to file2

mv file1 directory1 - Moves file1 into the directory directory1


more/less (File Content Viewer):#

Description:

The command more allows you to view the content of a text file one page at a time. Similar to more, the command less lets you view the content of text files one page at a time.

Example:

more file1 displays the contents of a text file named file1.


grep (Global Regular Expression Print):#

Description:

The command grep searches for lines containing a specified pattern within one or more text files. Useful for text searching and pattern matching.

Example:

grep hello file.txt searches for the string hello in the file.txt file and displays all lines containing that string.

If you want to search for a string recursively within all files that are in the directory directory1 you can use the -r flag i.e.,

grep -r hello directory1


wildcards (*):#

Description:

The symbol * can be used as a wildcard in unix. The * can be used to represent any combination of characters. For example, suppose you want to search for the string hello but only in files that end in .txt, you can use the following

grep hello *.txt

This will search for the string hello in all files that end with the .txt extension.