- Print Working Directory:
pwd: Shows the current directory you’re in.
- Change Directory:
cd [directory]: This changes your current directory.- For example:
cd Documentswill change your current directory to the „Documents“ folder.
- For example:
cd ..: Moves you up one directory.cd: Without any arguments, this command takes you to your home directory.cd ~: Also takes you to your home directory.cd -: Takes you to the previous directory you were in.
- List Files and Directories:
ls: Lists files and directories in the current directory.ls -l: Lists in long format, showing additional information like permissions, number of links, owner, group, size, and time of last modification.ls -a: Lists all entries including hidden files (those starting with a dot).ls -la: Combines both the above options.ls [directory]: Lists files and directories in the specified directory.
- Absolute vs. Relative Paths:
- Absolute Path: Refers to the exact path from the root directory (begins with a
/). For example:/Users/yourusername/Documents - Relative Path: Refers to the path relative to the current directory. For example, if you’re in
/Users/yourusername, you can just typecd Documentsinstead of the full absolute path.
- Absolute Path: Refers to the exact path from the root directory (begins with a
- Creating Directories:
mkdir [directory-name]: Creates a new directory.
- Removing Directories:
rmdir [directory-name]: Removes an empty directory.rm -r [directory-name]: Removes a directory and its contents. Use with caution!
- Path Autocompletion:
- When typing out paths, you can use the
Tabkey for autocompletion. For instance, typingcd Docand then pressingTabwill autocomplete tocd Documents/(assuming „Documents“ is the only directory starting with „Doc“).
- When typing out paths, you can use the
Creating Files
- touch:
- The
touchcommand is the easiest way to create new, empty files. It can also be used to change the timestamps on existing files.bashCopy codetouch filename.txtThis will create an empty file namedfilename.txtin the current directory.
- The
- echo:
- You can use the
echocommand to put content into a file.bashCopy codeecho "Hello, World!" > hello.txtThis will create a file namedhello.txtwith the content „Hello, World!“. Ifhello.txtalready exists, this command will overwrite it.If you want to append to the file instead of overwriting, use>>:bashCopy codeecho "Another line of text" >> hello.txt
- You can use the
- printf:
- Similar to
echo, but offers more formatting options.bashCopy codeprintf "Hello, World!\n" > hello.txt
- Similar to
- cat with redirection:
- The
catcommand can be used to display the content of files, but combined with the redirection operator, it can also create files.bashCopy codecat > filename.txtAfter executing this command, you can start typing content for the file directly into the terminal. Once done, pressCtrl + D(EOF) to save the content and exit.
- The
- nano, vi, or vim:
- These are popular command-line text editors. You can use them to create and edit files.bashCopy code
nano filename.txtThis will open thenanotext editor. After adding content, you can save and exit by pressingCtrl + Ofollowed byCtrl + X.Forviorvim:bashCopy codevi filename.txtOrbashCopy codevim filename.txtAfter making changes inviorvim, pressEsc, type:wqand pressEnterto save and exit.
- These are popular command-line text editors. You can use them to create and edit files.bashCopy code
- Using a file descriptor:
- You can create a file by opening a file descriptor and then closing it.bashCopy code
> filename.txtThis is somewhat less commonly used but is a neat trick. It creates an empty file namedfilename.txt.
- You can create a file by opening a file descriptor and then closing it.bashCopy code