Linux (2)

Linux Basics

File and Directory Commands
  1. Show size of files/folders:

    • Command: du -h

  2. Display contents of a file with line numbers:

    • Command: nl filename

  3. Execute an executable file:

    • Command: ./filename

    • Example: ./script.sh

  4. Set permissions with chmod:

    • Command: chmod <permissions> <file>

    • For user execute: chmod u+x file

    • For group write: chmod g+w file

  5. Show available free space on the disk:

    • Command: df -h

  6. Check last modification date and time of a file:

    • Command: stat filename

  7. Difference between ls -l and ls -a:

    • ls -l: Lists files in long format showing permissions, owners, size, modification date.

    • ls -a: Lists all files including hidden files.

Directory Structure and Navigation
  1. Display structure of a directory as a tree:

    • Command: tree

  2. Run a command in the background:

    • Command: command &

    • Example: ./myprogram &

  3. Display last 20 executed commands:

    • Command: history | tail -20

  4. Clear the terminal screen:

    • Command: clear

  5. Redirect command output to a file:

    • Command: command > filename

  6. Create a symbolic link:

    • Command: ln -s /path/to/file /path/to/symlink

  7. Find all files of a specific size in a directory:

    • Command: find . -size +100M (for files larger than 100MB)

  8. Delete all .log files in the current directory:

    • Command: rm *.log

  9. Check installed packages using apt:

    • Command: apt list --installed

  10. Create a bash script and make it executable:

    • Create script: nano script.sh

    • Add commands, save and exit.

    • Make it executable: chmod +x script.sh

  11. Automate script execution on startup:

    • Add to /etc/rc.local or create a systemd service.

Shell Configuration and Command Execution
  1. Purpose of .bashrc file:

    • A script that runs when a new terminal session starts, used for configuring shell environment.

  2. Enable error output in a bash script:

    • Use set -e to stop on errors, or set -x to print commands before execution.

  3. Find the current user:

    • Command: whoami

  4. Terminate a process:

    • Command: kill <PID>

    • Use ps or top to find PID.

  5. Change user password:

    • Command: passwd

  6. Display contents with line numbers from a specific line:

    • Command: sed -n '5,$=' filename (starting from line 5)

  7. Move a file from one directory to another:

    • Command: mv /path/to/file /path/to/destination

  8. Combine contents of multiple files into one:

    • Command: cat file1 file2 > combinedfile

  9. Find files modified in the last 7 days:

    • Command: find . -type f -mtime -7