Files
freeCodeCamp/guide/english/linux/basic-linux-commands/index.md

105 lines
7.5 KiB
Markdown
Raw Normal View History

---
title: Basic Linux Commands
---
## Basic Linux Commands
When starting out with Linux, there are some basic commands everyone should know.
 1. **cd** - change directory
- `cd` followed by a directory or file path will take you inside that directory (folder).
2018-11-20 14:42:45 +01:00
2. **ls** - list command
- Type `ls` and the contents of the current directory will be displayed.
- Two common flags used with `ls` are `-l` and `-a`, they can be used together and chained as such: `ls -la`. The `-l` flag will show you files in list order and in long format including a display of permissions, `-a` will show dot type files which are typically hidden in a GUI file finder such as `.env` files
- Flag ordering does not matter, so the previous command could be written as `ls -al`
2018-11-20 14:42:45 +01:00
3. **man** - manual command
- Shows you the manual for the following command. This is very helpful when trying to figure out how an unfamiliar command works. For example, type `man ls` for everything you need to know about the ls command. Type `q` to exit.
2018-11-20 14:42:45 +01:00
4. **pwd** - path
- Type `pwd` to display the path to your current directory.
2018-11-20 14:42:45 +01:00
5. **mkdir** - make directory
- This command, followed by the name you wish to name your directory, creates a new directory. `mkdir folder1` will make a new directory called folder1.
2018-11-20 14:42:45 +01:00
6. **rmdir** - remove directory
- Removes the directory that follows the command. `rmdir folder1` will delete the directory named folder1 if it exists.
2018-11-20 14:42:45 +01:00
7. **rm** - remove
- This command removes files, not directories. `rm file.txt` will remove the file named file.txt as long as it exists and is in the current directory.
2019-01-10 22:34:23 +07:00
- To remove directories that have files inside use `rm -r DIRECTORY_NAME`. `r` mean recursive and will delete both directory and files inside.
- **Warning, this command is capable of destroying entire systems, use with caution**
8. **touch** - sets modification and access times of files, creates new files
- The touch command sets the modification and access times of files to the current time by default. To set the access time and/or modification time of files to a different date or that of another file, certain flags can be used with the command. If the touch command is used with a filename that does not exist, it creates the file. `touch new.txt` will create a new text file with the name `new`.
2018-11-20 14:42:45 +01:00
9. **mv** - move
- Use the `mv` command to move files through the command line. We can also use the mv command to rename a file. For example, if we want to rename the file “text” to “new”, we can use `mv text new`.
- **Warning, this command is capable of destroying entire systems, use with caution**
2018-11-20 14:42:45 +01:00
10. **right-click** - copy and paste
- This one is less of a command and more of a how-to, however, it is very useful for doing almost anything in a terminal on Linux. To begin, highlight text like normal and then "right-click" on your mouse to copy a selection. You should see the highlighted portion become un-highlighted, this means you copied the selection. Now "right-click" on where you want to paste the selection and you're done.
2018-11-20 14:42:45 +01:00
11. **less** - view file content
- Use `less filename.txt` to view contents of a file and navigate through them. By default, less will go through the file page by page.
2018-10-14 20:43:24 +01:00
2018-11-20 14:42:45 +01:00
12. **cat** - display file's textual contents
- Use this command to display text from within a given file on your terminal. Entering `cat myFile.txt` in your terminal will display the contents of the file `myFile.txt` on screen. The `cat` command can be a very handy tool when used with the terminal _pipes_.
2018-11-20 14:42:45 +01:00
13. **clear** - clear terminal
2018-10-14 20:43:24 +01:00
- Use `clear` to simply clear all output from your terminal.
2018-11-20 14:42:45 +01:00
14. **cp** - copy files and directories
- Use 'cp' to copy a file or a direcotry with files inside it to another location using command `cp CURRENT_FILE-LOCATION DESTINATION_FOLDER`. Add '-r' flag to copy a directory that is not empty.
2018-12-13 18:37:05 -08:00
15. **du** - estimate file space usage
2018-11-22 22:54:19 +07:00
- Use 'du' to estimate file space usage. du is abbreviation of "disk usage". This command tool reports usage by given directory
2018-12-13 18:37:05 -08:00
16. **ln** - link LINKNAME to TARGET file/directory
2018-11-20 14:42:45 +01:00
- Link is pointer which connects file name to actual bytes of data on the disk. More than one link can point on the same data.
- Use `ln` to create a link to targeted file with linkname. Link to target is created in current working directory. By default ln creates [hard link](http://www.linfo.org/hard_link.html) and requires existance of TARGET file/directory. If we add `-s`(--symbolic) to the command, [symbolic link](https://www.computerhope.com/jargon/s/symblink.htm) will be created.
2018-12-13 18:37:05 -08:00
17. **top** - shows the system load
- Use `top` to get information about running processes of your system, the amount or RAM they allocate and the CPU percentage that they use. 'Top' is interactive so do not forget to press `q` when you want to quit.
2018-12-13 18:37:05 -08:00
18. **df -h** Checks disk space in human readable form
- Shows the size, amount used, amount available and capacity percentage of mounted drives/partitions.
2018-12-13 18:37:05 -08:00
19. **grep** - The grep searches any given input files, selecting lines that match one or more patterns.
- use `grep` to find a file, a directory, some text in the file/directory.
2018-11-20 14:42:45 +01:00
2018-12-13 18:37:05 -08:00
20. **sudo** - execute a command as super user
- A widely used command in the Linux command line, sudo stands for "SuperUser Do". So, if you want any command to be done with administrative or root privileges, you can use the sudo command.
2018-12-13 18:37:05 -08:00
21. **kill** - stops runing process
- The `kill` command sends a signal to a running process. This default action normally stops processes. If you want to stop a process, specify the process ID (PID) in the `ProcessID` variable. The shell reports the PID of each process that is running in the background (unless you start more than one process in a pipeline, in which case the shell reports the number of the last process). You can also use the `ps` command to find the process ID number of commands.
2018-12-13 18:37:05 -08:00
22. **find** - search for files in a directory hierarchy
- `find` searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for and operations, true for or), at which point `find` moves on to the next file name.
2018-12-13 18:37:05 -08:00
23. **man** - Show the manual for a command
- Use `man` to see manual of any commmand. It will show different ways a given command can be used
Example: `man ls`
2018-12-13 18:37:05 -08:00
24. **ps** - Show active processes
- Use `ps` to see running processes, and their process IDs
Example: `ps`
2018-12-13 18:37:05 -08:00
25. **history**
- Use `history` to see previously entered commands
2018-11-20 14:42:45 +01:00
Example: `history`
26. **passwd** - changes user passwords
- Use `passwd -h` to look at the options available for changing passwords.
27. **whoami** - display current user
- Use this command to display the username of the current user to the console.
28. **chmod** - change permission
- Use 'chmod' to make a file executable and to change the permissions granted to it in Linux. For the example , imagine you have a file named `myFile.txt` . But it can't executable because you not have a permission to this file , you can use the command `chmod +x myFile.txt` or `chmod 755 myFile.txt`.
### Useful Resources for Practice:
- [JSLinux](https://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/buildroot-x86.cfg): Run the terminal inside your browser. Great for practice.
- [LearnShell](https://www.learnshell.org/): Interactive Linux shell (terminal) tutorials.
- [LinuxJourney](https://linuxjourney.com/lesson/the-shell): A collection of beginner-friendly terminal tutorials.