Tuesday 17 October 2023

D03: Basic Commands in Linux

  •  The commands are also known as "Programs" since written commands are being executed
Linux Command

1. ls
  • Lists the files and directories in the system
  • Syntax: 
    • ls [/directory/folder/path]
  • If you remove the path, the ls command will show the current working directory’s content. You can modify the command using these options:
    • -R – lists all the files in the subdirectories.
    • -a – shows all files, including hidden ones.
    • -lh – converts sizes to readable formats, such as MB, GB, and TB.
    • ls  - a - command to view hidden files
    • ls -al - gives detailed information of the files. The command provides information in a columnar format

2. Creating and viewing files
  • Concatenate or cat is one of the most used Linux commands. It lists, combines, and writes file content to the standard output. 
  • Syntax:
    • cat filename.txt
  • There are various ways to use the cat command:
    • cat > filen.txt – creates a new file.
    • cat file1.txt file2.txt > file3.txt – merges file1.txt with file2.txt and stores the output in filename3.txt.
    • tac file.txt – displays content in reverse order.
3. Deleting Files
  • Use the rm command to permanently delete files within a directory. 
  • Syntax:
    • rm [filename1] [filename2] [filename3]
  • Adjust the number of files in the command according to your needs. If you encounter an error, ensure you have the write permission in the directory.
  • To modify the command, add the following options:
    • -i – prompts a confirmation before deletion.
    • -f – allows file removal without a confirmation.
    • -r – deletes files and directories recursively.
  • Note* Use the rm command with caution since deletion is irreversible. Avoid using the -r and -f options since they may wipe all your files. Always add the -i option to avoid accidental deletion.
4. Moving files
  • Use the mv command to move or rename files and directories. To move items, enter the file name followed by the destination directory
    • mv filename.txt /home/username/Documents
  • Meanwhile, use the following syntax to rename a file in Linux with the mv command:
    • mv old_filename.txt new_filename.txt
  • mv command needs super user/ root user permission.  Currently, the command is executed as a standard user ,so above error is thrown
  • To overcome the error use command sudo
  • sudo:
    • program allows regular users to run programs with the security privileges of the super user or root user
    • Superuser do or sudo is one of the most basic commands in Linux. It runs your command with administrative or root permissions. Here’s the general syntax:
      • sudo (command)
    • When you run a sudo command, Terminal will request the root password. For example, this snippet runs useradd with the superuser privilege:
      • sudo useradd username
    • You can also add an option, such as:
      • -k – invalidates the timestamp file.
      • -g – executes commands as a specified group name or ID.
      • -h – runs commands on the host.
    • Notes*: Running a command with sudo privileges can modify all aspects of your system. Since misusing it may break your system, run the command with caution and only if you understand its possible repercussions.
5. Renaming Files
  • mv can also be used for renaming the file.  The syntax is
    • mv filename newfilename
6. Directory Manipulations
  • Use the mkdir command to create one or multiple directories and set their permissions. Ensure you are authorized to make a new folder in the parent directory. Here’s the basic syntax:
    • mkdir [option] [directory_name]
  • To create a folder within a directory, use the path as the command parameter. For example, mkdir music/songs will create a songs folder inside music. Here are several common mkdir command options:
    • -p – creates a directory between two existing folders. For example, mkdir -p Music/2023/Songs creates a new 2023 directory.
    • -m – sets the folder permissions. For instance, enter mkdir -m777 directory to create a directory with read, write, and execute permissions for all users.
    • -v – prints a message for each created directory.
  • rmdir- command is used to remove a directory
  • mv -  'mv' (move) command can also be used for renaming directories
  • Syntax of mv command-
    • mv directoryname newdirectoryname
7. Search Command
  • The global regular expression or grep command lets you find a word by searching the content of a file. This Linux command prints all lines containing the matching strings, which is useful for filtering large log files.
  • For example, to display lines containing blue in the notepad.txt file, enter:
    • grep blue notepad.txt
Other important Commands
  • History: 
    • Enter history to list previously executed commands. It lets you reuse the commands without rewriting them. To use it, enter this syntax with sudo privileges:
      • history [option]
  • Clear:
    • command clears all the clutter on the terminal and gives you a clean window to work on
  • pwd:
    • The pwd command prints your current working directory’s path, like /home/directory/path. Here’s the command syntax:
      • pwd [option]
    • It supports two options. The -L or –-logical option prints environment variable content, including symbolic links. Meanwhile, -P or –physical outputs the current directory’s actual path.
  • cd- :
    • Use the cd command to navigate the Linux files and directories. To use it, run this syntax with sudo privileges:
      • cd /directory/folder/path
    • Depending on your current location, it requires either the full path or the directory name. For example, omit /username from /username/directory/folder if you are already within it.
    • Omitting the arguments will take you to the home folder. Here are some navigation shortcuts:
      • cd ~[username] – goes to another user’s home directory.
      • cd .. – moves one directory up.
      • cd- – switches to the previous directory.
  • touch- :
    • The touch command lets you create an empty file in a specific directory path. Here’s the syntax:
      • touch [option] /home/directory/path/file.txt
    • If you omit the path, the command will create the item in the current folder. You can also use touch to generate and modify a timestamp in the Linux command line.
  • su- :
    • The su command lets you run a program in the Linux shell as a different user. It is useful to connect via SSH while the root user is disabled. Here’s the syntax:
      • su [options] [username [argument]]
    • Without any option or argument, this command runs through root and prompts you to use the sudo privileges temporarily. Some options are:
      • -p – keeps the same shell environment, consisting of HOME, SHELL, USER, and LOGNAME.
      • -s – lets you specify another shell environment to run.
      • -l – runs a login script to switch users. It requires you to enter the user’s password.

No comments:

Post a Comment

G01:Client Server Architecture

  G01 Client Server Architecture A central server (host computer) serves as both the source of service requests and the destination for ...