Linux (2)
Linux Basics
File and Directory Commands
Show size of files/folders:
Command:
du -h
Display contents of a file with line numbers:
Command:
nl filename
Execute an executable file:
Command:
./filenameExample:
./script.sh
Set permissions with chmod:
Command:
chmod <permissions> <file>For user execute:
chmod u+x fileFor group write:
chmod g+w file
Show available free space on the disk:
Command:
df -h
Check last modification date and time of a file:
Command:
stat filename
Difference between
ls -landls -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
Display structure of a directory as a tree:
Command:
tree
Run a command in the background:
Command:
command &Example:
./myprogram &
Display last 20 executed commands:
Command:
history | tail -20
Clear the terminal screen:
Command:
clear
Redirect command output to a file:
Command:
command > filename
Create a symbolic link:
Command:
ln -s /path/to/file /path/to/symlink
Find all files of a specific size in a directory:
Command:
find . -size +100M(for files larger than 100MB)
Delete all .log files in the current directory:
Command:
rm *.log
Check installed packages using apt:
Command:
apt list --installed
Create a bash script and make it executable:
Create script:
nano script.shAdd commands, save and exit.
Make it executable:
chmod +x script.sh
Automate script execution on startup:
Add to
/etc/rc.localor create a systemd service.
Shell Configuration and Command Execution
Purpose of
.bashrcfile:A script that runs when a new terminal session starts, used for configuring shell environment.
Enable error output in a bash script:
Use
set -eto stop on errors, orset -xto print commands before execution.
Find the current user:
Command:
whoami
Terminate a process:
Command:
kill <PID>Use
psortopto find PID.
Change user password:
Command:
passwd
Display contents with line numbers from a specific line:
Command:
sed -n '5,$=' filename(starting from line 5)
Move a file from one directory to another:
Command:
mv /path/to/file /path/to/destination
Combine contents of multiple files into one:
Command:
cat file1 file2 > combinedfile
Find files modified in the last 7 days:
Command:
find . -type f -mtime -7