Basic Commands in Linux
Commands
Note:
- Directories are denoted in blue colour
- Files are denoted in white
- Similar colour schemes used in different distributions of Linux
- Suppose, your "Music" folder has the following sub-directories and files
- command is case-sensitive
Listing files(ls): to see the list of files on a UNIX or Linux system, use the 'ls' command
It shows the files /directories in the current directory.
ls -R: shows all the files not only in directories but also in subdirectories.
ls -al -(give info of the file): The command provides information in a columnar format.
In UNIX/Linux, any file or directory that starts with a period symbol (.) is considered a hidden item. Such items are not visible unless they are explicitly requested. This is an important aspect to keep in mind while working with UNIX/Linux systems.
Creating and viewing files
cat: The 'cat' command displays, copies, combines, and creates text files.
To create a new file, use the command
- cat >filename
- Add content
- Press 'ctrl + d' to return to the command prompt
To view a file, use the command cat
To view the file that has been created, you can use the command "cat filename".
Create another sample
then the syntax to combine two files is cat file1 file2 > newfilename
Then as soon as the user inserts the above command and hits enter, the files are concatenated, but the user does not see a result.
To execute commands in Bash Shell (Terminal), you won't receive any confirmation like "OK" or "Command Successfully Executed". However, if something goes wrong or an error occurs, you'll see a message. To view the newly created combo file "sample", use the command "cat sample".
only text files can be displayed and combined using this command
Deleting files
rm: command removes files from the system without confirmation.
mv: command to move a file to a different directory. mv command needs superuser/ root user permission.
sudo program allows regular users to run programs with the security privileges of the super user or root user.
Moving and re-naming files
When you use the sudo command, you will be asked to authenticate with a password. However, you don't have to know the root password. You can use your own password instead. Once you're authenticated, the system will run the command you requested. Sudo keeps a log of every command that's run, so system administrators can trace any unwanted changes in the system back to the person responsible.
When renaming a file, use the "mv" command followed by the file's current name and the desired new name. The syntax is: mv currentfilename newfilename.
The sudo password is automatically saved for 15 minutes per terminal by default. This saves you from having to enter the password repeatedly. You only need root/sudo privileges when you are executing commands that involve files or directories that are not owned by the user or group running the command.
Directory Manipulations
mkdir: On a Linux operating system, the command "mkdir" is used to create directories. This command creates a subdirectory in the current working directory, which is usually the user's home directory.
To create a directory in a location other than the home directory, such as '/tmp', use the command 'mkdir /tmp/MUSIC' to create a 'Music' directory.
Removing directories
rmdir: rmdir command is used to remove a directory. To delete a directory named "mydirectory", type rmdir mydirectory.
Tip: Ensure that there is no file / sub-directory under the directory that you want to delete. Delete the files/sub-directory first before deleting the parent directory
Renaming directories
mv command can be used to rename directories. Syntax: mv directoryname newdirectoryname.
Search Command
grep(global regular expression print): Text can be searched and printed using regular expressions.
If you need help with the command 'grep', you can type 'man grep'. For instance, if you want to search for the word "apple" in the file 'fruits.txt', you can use the command 'grep "apple" fruits.txt'. On the other hand, 'find' is used to search for files and directories.
History: The history command displays previously entered commands for the current terminal session, allowing users to easily reference and reuse them.
Clear: The command clears all the clutter on the terminal and gives you a clean window to work on.
Text Editor
Vi (Visual Editor) or Vim (Vi Improved) is a terminal-based text editor that allows you to write files. You can use the commands "vi" or "vim" to work with it. To learn how to use it, you can download the VI editor file from the VLE.
There is also a built-in text editor called Nano, which you can explore on your own. If you prefer a GUI-based text editor, you can use the built-in software called Gedit. To launch Gedit, simply type "gedit" in the command line. To open a file in Gedit, use the command "gedit filename".
0 Comments