0% found this document useful (0 votes)
25 views7 pages

Ubuntu Commands

The document provides a comprehensive guide on various command-line operations in a Unix-like environment, covering file and directory management, text editing, and scripting. It includes commands for creating, moving, copying, and deleting files, as well as using text processing tools like grep, awk, and sort. Additionally, it explains basic scripting concepts such as loops, conditionals, and user input handling.

Uploaded by

teeteevisual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Ubuntu Commands

The document provides a comprehensive guide on various command-line operations in a Unix-like environment, covering file and directory management, text editing, and scripting. It includes commands for creating, moving, copying, and deleting files, as well as using text processing tools like grep, awk, and sort. Additionally, it explains basic scripting concepts such as loops, conditionals, and user input handling.

Uploaded by

teeteevisual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

pwd → path name of the current directory

cd desktop/ → change directory to desktop

mkdir → create directory/ folder

cd customer
touch [Link] → create a text file inside customer folder called index

ls → shows all the files and directories inside a particular folder

ls -a→ shows only the files inside a particular directory


ls -d → displays only the directories inside a given path
ls -l → displays the long format listing
cd .. → will take us back to the parent folder
cd / → will take us back to one of the main(root) directories

1. vi [Link] → opens vi editor in the command mode with a text file named seller
2. i → changes the mode from command mode to insertion mode
3. insert the content (text → seller is selling vegetables)
4. esc → change the insertion mode back to the command mode
5. :wq → save the content inside the [Link] file and exit the vi editor
:q! → when the entered content is not needed to be saved

cat [Link] → shows the content inside the seller file


more [Link] → can be used to read what’s inside the seller file just like the cat command
wc [Link] → shows the number of lines, words and the characters in the seller text file respectively

mv [Link] [Link] → move all the contents of the seller folder to products folder and
automatically delete the seller folder and the content that was previously in the products folder

cp [Link] [Link] → copy the content what’s inside the products folder to the copy folder

rm [Link] → remove/ delete the goods text file


rmdir salary → removes/ deletes the salary directory
rm -R employee → deletes a directory which has files in it

history → show all the commands


ifconfig → check machine ip addresses
who → shows the username current time and date
whoami → shows the username
ps → the process ids of the processes that are currently running

chmod ug+x [Link] → for the user and the group execution permissions are also given

uname -a → shows a summary about the system


echo → works as the printf in c
echo myName → output will be myName
echo $myName → output will be the value stored in myName
echo `date` → back quotes works as a command and prints out the date

echo $BASH → displays the shell name


echo $USER → displays the username
echo $((5+10)) → output will be 15
expr 1 + 4 → output will be 5 (the spaces between are mandatory)
nano [Link] → opens a shell names test01
“ctrl + x” then press “y” then press “enter” to save the content written in the shell

1. chmod +x [Link]
2. ./[Link]
This order of commands execute what’s written in the shell

read -p “Enter two numbers”x y → enter values for x and y on the same line (-p)

read -a names
echo “Names: ${names[0]}, ${names[1]}, ${names[2]}“

-eq → is equal to
-ne → is not equal to
-lt → is less than
-le → is less than or equal to
-gt → is greater than
-ge → is greater than or equal to

Value=20
If [ $value -eq 20 ]
Then
echo “condition is true.”
Fi
Ps: there should be a space in between every element after the if word.

#!/bin/bash
count=5
while [ $count -le 15 ]
do echo $count
((count++))
Done
#!/bin/bash
for i in 1 2 3 4 5
do
echo “Welcome $i times”
done

#!/bin/bash
vehicle=”van”
case $vehicle in
“car”) echo “rent for $vehicle is 5000 rupees”;;
“van”) echo “rent for $vehicle is 10000 rupees”;;
“bicycle”) echo “rent for $vehicle is 1000 rupees”;;
*)
echo “unknown vehicle”;;
esac

1. cat > animals


2. dog
lion
deer
snake
tiger
eagle
cat
3. “ctrl + d” → to save the above list
4. sort animals → to sort the animals in the alphabetical order
5. cat
deer
dog
eagle
lion
snake
tiger
1. sort – animals
2. rat
fish
bird
adding new animals to the list
3. “ctrl + d”
4. bird
cat
deer
dog
eagle
fish
lion
eagle
snake
tiger

sort -r animals → this will sort the list of animals from tiger to cat without including the animals we
added later. (without including rat, fish and bird)

sort -n animals → sorting the animals according to the number in the ascending order
sort -nr animals → sorting the animals according to the number in the descending order
sort -oabc animals → create a new file called abc with the same contents as in animals by sorting them
according to the number

1. cat > employee


2. manager 4000
teacher 300
clerk 20
3. “ctrl + c”
4. sort -k 2 [Link]
5. clerk 20
teacher 300
manager 4000

sort -t “|” -k 2 animals


4| cat
5| deer
123| dog
2| eagle
3| lion
1| snake
9| tiger
echo I like cricket | cut -b 2 → I
echo I like cricket | cut -b 5 → k

for i in $(seq 10); do echo $(seq -s ‘ ‘ 1 9) >> [Link]; done


output → 1 2 3 4 5 6 7 8 9
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
-s represents a space in between the numbers.

cut -d ‘ ‘ -f 2 [Link] → cut the 2nd field from the above number text
output → 2
2
2
2
2
2
2
2
2
2

cut -d ‘ ‘ -f 4-9 [Link] → from 4 to 9 rows will be printed

cut -d ‘ ‘ -f -2,4- [Link] → 3rd row will be hidden


cut -d ‘ ‘ -f -1,3- [Link] → 2nd row will be hidden
cut -d ‘ ‘ -f 3 –complement [Link] → 3rd row will be hidden

echo 123456789 >> [Link] → 123456789 will be added after the above sequence of numbers

cut -s -d ‘ ‘ -f 3 [Link] → care only the numbers with a delimiter

tab or : → field seperators

cut -f 2,3 -d”:” [Link] → extract 2 and 3 rows

cut -c 1-4 [Link] →from saman the first 4 bytes “sama” will be displayed
grep love [Link] → extract the word “love” with the statements itself
grep -i love [Link] → extract the word love including the “Love” as well
grep -i -n -v love [Link] → extract the sentences without the word love in it
grep -I -n -v -c love [Link] → count the number of sentences without the word love in it

awk ‘{print “welcome to awk lab session”}’

cat /etc/passwd

awk -F ‘:’ ‘{total += NF}; END {print total}’ /etc/passwd → prints the number of fields in the passwd file

date |awk ‘OFS=”/” {print $2,$3,$6}’

awk ‘BEGIN{print “this is my first lesson”}{print $1,$2,$6}END{print “this is end”}’ [Link]

awk ‘$4 ~/Tech/’ [Link] → display the information of the employees under the tech department

echo "Hello John|awk '{$2=”Andrew”; print $0}’ → Hello John will be changed into Hello Andrew

awk ‘length($2)>5’ [Link] → displays the details of the employees whos name has more than 5
characters (the 2nd column is the employee’s name)

echo -e “One Two\nOne Two Three\nOne Two Three Four” | awk ‘NF > 2’ → displays the texts which
has more than 2 fields
-e eliminates special characters like \n

echo -e “cat\nbat\nfin\fan\nfun” | awk ‘/f.n/’ → displays the words that starts from f and ends from n

echo -e “call\ntall\nball” | awk ‘/[^ct]all/’ → output will be ball

echo -e “Colour/Color” | awk ‘/Colou?r/’ → r will be optional in this case so both Colour and Color will
be printed

echo -e “ca\ncat\ncatt” | awk ‘/cat*/’ → from the * it shows the that zero or more than that amount of
“t” s can appear so all three ca, cat and catt will be printed

echo -e “Apple Juice\nApple Pie\nApple Tart\nApple Cake” | awk ‘/Apple (Cake|Juice)/’


output → Apple Juice
Apple Cake

awk ‘BEGIN{print index(“peanut”, “an”)}’ → 3 will be the output


for item in *
do
if [ -f $item ]
then
echo $item
fi
done

count=5
while [ $count -le 15 ]
do
echo $count
((count++))
done

You might also like