Unix commands are a set of commands that are used to interact with the Unix operating system. Unix is a powerful, multi-user, multi-tasking operating system that was developed in the 1960s by Bell Labs. Unix commands are entered at the command prompt in a terminal window, and they allow users to perform a wide variety of tasks, such as managing files and directories, running processes, managing user accounts, and configuring network settings. Unix is now one of the most commonly used Operating systems used for various purposes such as Personal use, Servers, Smartphones, and many more. It was developed in the 1970’s at AT& T Labs by two famous personalities Dennis M. Ritchie and Ken Thompson.
- You’ll be surprised to know that the most popular programming language C came into existence to write the Unix Operating System.
- Linux is Unix-Like operating system.
- The most important part of the Linux is Linux Kernel which was first released in the early 90s by Linus Torvalds.There are several Linux distros available (most are open-source and free to download and use) such as Ubuntu, Debian, Fedora, Kali, Mint, Gentoo, Arch and much more.
- Now coming to the Basic and most usable commands of Linux/Unix part. (Please note that all the linux/unix commands are run in the terminal of a linux system.Terminal is like command prompt as that of in Windows OS)
- Linux/Unix commands are case-sensitive i.e Hello is different from hello.
Basic unix commands:
1. who : The ‘$ who’ command displays all the users who have logged into the system currently. As shown above, on my system I am the only user currently logged in.The thing tty2 is terminal line the user is using and the next line gives the current date and time
Output: harssh tty2 2017-07-18 09:32 (:0)
2. pwd : The ‘$pwd’ command stands for ‘print working directory’ and as the name says,it displays the directory in which we are currently (directory is same as folder for Windows OS users).
In the output, we are harssh directory(folder for Windows OS that are moving to Linux),which is present inside the home directory.
3. mkdir : The ‘$ mkdir’ stands for ‘make directory’ and it creates a new directory.We have used ‘$ cd’ (which is discussed below) to get into the newly created directory and again on giving ‘$ pwd’ command,we are displayed with the new ‘newfolder’ directory.
$ mkdir newfolder
$ cd newfolder
4. rmdir : The ‘$ rmdir’ command deletes any directory we want to delete and you can remember it by its names ‘rmdir’ which stands for ‘remove directory’.
$ rmdir newfolder
5. cd : The ‘$ cd’ command stands for ‘change directory’ and it changes your current directory to the ‘newfolder’ directory.You can understand this a double-clicking a folder and then you do some stuff in that folder.
$ cd newfolder (assuming that there is a directory named 'newfolder' on your system)
6. ls : The ‘ls’ command simply displays the contents of a directory.
Output: Desktop Documents Downloads Music Pictures Public Scratch Templates Videos
7. touch : The ‘$ touch’ command creates a file(not directory) and you can simple add an extension such as .txt after it to make it a Text File.
$ touch example
Output: Desktop Documents Downloads Music Pictures Public Scratch Templates Videos example
Note: It is important to note that according to the Unix File structure, Unix treats all the stuff it has as a ‘file’, even the directories(folders) are also treated as a file.You will get to know more about this as you will further use Linux/Unix based OS 🙂
8. cp : This ‘$ cp ‘ command stands for ‘copy’ and it simply copy/paste the file wherever you want to.In the above example, we are copying a file ‘file.txt’ from the directory harssh to a new directory new.
$ cp /home/harssh/file.txt /home/harssh/new/
9. mv : The ‘$ mv’ command stands for ‘move’ and it simply move a file from a directory to another directory.In the above example a file named ‘file.txt’ is being moved into a new directory ‘new’
$ mv /home/harssh/file.txt /home/harssh/new
10. rm : The ‘$ rm ‘ command for remove and the ‘-r’ simply recursively deletes file. Try ‘$ rm filename.txt’ at your terminal 🙂
$ rm file.txt
11. chmod : The ‘$ chmod’ command stands for change mode command.As there are many modes in Unix that can be used to manipulate files in the Unix environment.Basically there are 3 modes that we can use with the ‘chmod’ command
1. +w (stands for write and it changes file permissions to write)
2. +r (stands for read and it changes file permissions to read)
3. +x (generally it is used to make a file executable)
$ chmod +w file.txt
$ chmod +r file.txt
$ chmod +x file.txt
12. cal : The ‘$ cal’ means calendar and it simply display calendar on to your screen.
Output : July 2017
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
13. file : The ‘$ file’ command displays the type of file.As I mentioned earlier Linux treats everything as a file so on executing the command file on a directory(Downloads) it displays directory as the output
Output: Desktop Documents Downloads Music Pictures Public Scratch Templates Videos
$ file Downloads
Output: Downloads: directory
14. sort : As the name suggests the ‘$ sort’ sorts the contents of the file according to the ASCII rules.
$ sort file
15. grep : grep is an acronym for ‘globally search a regular expression and print it’.The ‘$ grep’ command searches the specified input fully(globally) for a match with the supplied pattern and displays it.
In the example, this would search for the word ‘picture’ in the file newsfile and if found,the lines containing it would be displayed on the screen.
$ grep picture newsfile
16. man : The ‘$ man’ command stands for ‘manual’ and it can display the in-built manual for most of the commands that we ever need.In the above example, we can read about the ‘$ pwd’ command.
$ man pwd
17. lpr : The ‘$ lpr’ command send a file to the printer for printing.
$ lpr new.txt
18. passwd : The ‘$ passwd’ command simply changes the password of the user.In above case ‘harssh’ is the user.
Output: Changing password for harssh.
(current) UNIX password:
19. clear : The ‘$ clear’ command is used to clean up the terminal so that you can type with more accuracy 🙂
20. history : The ‘$ history’ command is used to get list of previous commands may be obtained by executing the following command. you can also use parameters like !n to re-execute the nth command, !! to executes the most recent command, and !cp this will execute the most recent command that starts with cp.
At last, I want to say that these are the most basic and essential commands that are used in the Linux operating system. You will need them even if you get to advance the Unix. If you want to master them just keep on practicing them.
Also, it is not possible to cover all the Unix commands because they are so many in number. You can find more, just google it and you will find most of them. Also if you want to master Unix operating system, Learn Unix Shell Scripting/Bash Scripting. Trust me there are many awesome tutorials on the internet for them.
Compile your C/C++ program in the Unix terminal
First reach the directory where your .c or .cpp file is present (assume its name is new.c or new.cpp). Please note that in order to compile C you will need GCC compiler and in order to compile C++ you will need g++. I will tell you below how to install them.
$ gcc new.c -o new
$ g++ new.cpp -o new
In this way you can compile you programs of C and C++. You can even compile many more that i will cover in my coming articles.
Getting gcc, g++
For deb based Linux such as Ubuntu,Debian,Kali etc:
$ sudo apt-get install gcc
$ sudo apt-get install g++
For rpm based Linux such as Fedora,Oracle Linux etc:
$ dnf install gcc
$ dnf install g++
$ yum install gcc
$ yum install g++
This article is contributed by Harsh Wardhan Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Uses of Unix Commands :
- File and directory management: Unix commands like ls, cd, cp, mv, rm, mkdir, and rmdir are used for managing files and directories. These commands allow users to create, delete, copy, move, and rename files and directories.
- Process management: Unix commands like ps and kill are used for managing running processes. Users can use these commands to list all running processes, view process details, and terminate a process if necessary.
- User management: Unix commands like passwd, useradd, userdel, and groupadd are used for managing user accounts and groups. These commands allow system administrators to add or delete user accounts, change user passwords, and manage group memberships.
- Text manipulation: Unix commands like cat, grep, sed, and awk are used for manipulating text files. These commands allow users to view, search, replace, and format text files.
- Networking: Unix commands like ping, ifconfig, netstat, and traceroute are used for configuring and troubleshooting network connections. These commands allow users to test network connectivity, view network interface details, and diagnose network problems.
- System configuration: Unix commands like chmod, chown, and sysctl are used for configuring system settings. These commands allow users to change file permissions, file ownership, and system parameters.
- Programming: Unix commands like gcc, make, and gdb are used for compiling and debugging programs. These commands are essential for developers who write programs in C or C++.
Issues in Unix Commands :
- Command syntax errors: Unix commands are sensitive to syntax errors, which can lead to unexpected behavior or errors. Users must be careful to use the correct command syntax and to include all required parameters.
- Permissions issues: Unix commands are subject to file permissions, which can restrict access to certain files or directories. Users may need to use the chmod or chown commands to change file permissions or file ownership to access certain files.
- Security risks: Some Unix commands can be used to compromise system security, such as allowing unauthorized access to files or executing malicious code. Users must be careful to use Unix commands only for authorized tasks and to avoid running unknown scripts or commands.
- Lack of user interface: Unix commands are typically executed from the command line, which can be intimidating for users who are used to graphical user interfaces. Users may need to spend time learning the syntax and usage of Unix commands to be effective.
- Limited documentation: While Unix commands are well-documented, some commands may not have complete or clear documentation. Users may need to rely on online resources or community forums to find answers to specific issues.
Reference for Unix Commands:
There are many online resources that provide references and documentation for Unix commands. Some of the most popular references include:
- The Unix manual pages: The Unix manual pages, also known as “man pages,” provide detailed documentation for all Unix commands. Users can access the man pages by typing “man [command]” at the command prompt.
- Linux Documentation Project: The Linux Documentation Project provides a comprehensive collection of guides, how-tos, and FAQs for Unix and Linux users. The website is maintained by a community of volunteers and is available at https://www.tldp.org/.
- Bash Reference Manual: The Bash Reference Manual provides detailed documentation for the Bash shell, which is the default shell for most Unix-based systems. The manual is available online at https://www.gnu.org/software/bash/manual/bash.html.
- Unix Tutorial for Beginners: The Unix Tutorial for Beginners is a free online tutorial that provides an introduction to Unix commands and the Unix operating system. The tutorial is available at https://www.ee.surrey.ac.uk/Teaching/Unix/.
- Unix/Linux Command Reference: The Unix/Linux Command Reference is a comprehensive reference guide for Unix commands. The website provides a searchable index of commands and their usage, and is available at https://www.pixelbeat.org/cmdline.html.