Here are 15 basic Linux commands that are essential for everyday use:
1. **`ls`**: Lists directory contents.
ls
2. **`cd`**: Changes the current directory.
cd /path/to/directory
3. **`pwd`**: Prints the current working directory.
pwd
4. **`cp`**: Copies files or directories.
cp source_file destination_file
5. **`mv`**: Moves or renames files or directories.
mv old_name new_name
6. **`rm`**: Removes files or directories.
rm file_name
7. **`mkdir`**: Creates a new directory.
mkdir new_directory
8. **`rmdir`**: Removes an empty directory.
rmdir directory_name
9. **`touch`**: Creates an empty file or updates the timestamp of an existing file.
touch file_name
10. **`cat`**: Concatenates and displays the content of files.
cat file_name
11. **`nano`**: Opens the nano text editor to edit files.
nano file_name
12. **`chmod`**: Changes file permissions.
chmod 755 file_name
13. **`chown`**: Changes file owner and group.
chown user:group file_name
14. **`ps`**: Displays information about running processes.
ps aux
15. **`top`**: Displays real-time system information including running processes.
top
These commands form the basis for navigating and manipulating files, directories, and
system processes in a Linux environment.
Creating a file in Unix can be done using several different commands, depending on
your needs. Here are a few common methods:
1. **Using the `touch` command**: This creates an empty file or updates the timestamp
of an existing file.
touch filename
2. **Using the `echo` command**: This can create a file with some initial content.
echo "Initial content" > filename
3. **Using the `cat` command**: This allows you to type content into a file.
cat > filename
After entering the above command, type your content and then press `Ctrl+D` to
save and exit.
4. **Using the `nano` command**: This opens the nano text editor to create and edit a
file.
nano filename
After entering your content, press `Ctrl+O` to write out (save) the file, then `Ctrl+X`
to exit nano.
5. **Using the `vi` or `vim` command**: This opens the vi or vim text editor to create
and edit a file.
vi filename
Once in vi, press `i` to enter insert mode, type your content, then press `Esc` to exit
insert mode. Type `:wq` and press Enter to write (save) the file and quit vi.
6. **Using the `>` operator**: This creates an empty file.
> filename
Each method has its own advantages, whether you need an empty file, want to add
some initial content, or prefer to edit the file interactively.
script
echo "Hello, World!"
# User input script
echo "Enter your name:"
read NAME
echo "Hello, $NAME!"
# Loop script
for i in {1..5}; do
echo "Iteration $i"
done
# Simple calculator script
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
echo "Choose operation: + - * /"
read operation
case $operation in
+) result=$((num1 + num2)) ;;
-) result=$((num1 - num2)) ;;
*) result=$((num1 * num2)) ;;
/) result=$((num1 / num2)) ;;
*) echo "Invalid operation" ;;
esac
echo "Result: $result"
Running the Scripts
Save the script to a file, e.g., script.sh.
Make the script executable:
chmod +x script.sh
Run the script:
./script.sh