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

Week1 My Unix

Uploaded by

naninaidu586
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)
8 views7 pages

Week1 My Unix

Uploaded by

naninaidu586
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
You are on page 1/ 7

WEEK-1

1. Directory and File Handling Utilities

a. Display the current working directory


Pwd
*This command prints the current working directory to the terminal.
b. Create a directory named lab1
mkdir lab1
*This creates a directory named lab1 in the current location.

c. Navigate into the lab1 directory


cd lab1
*Changes the current working directory to lab1.

d. Create three subdirectories dir1, dir2, and dir3 inside lab1


mkdir dir1 dir2 dir3

● Creates three subdirectories under lab1.

e. List the contents of the lab1 directory


ls

● This will display the names of the subdirectories dir1, dir2, and dir3 inside lab1.

2. Basic File Operations

a. Create three files named file1.txt, file2.txt, and file3.txt inside dir1
touch dir1/file1.txt dir1/file2.txt dir1/file3.txt

● This creates three empty files inside dir1.

b. Copy file1.txt to dir2 and rename it to file1_copy.txt


cp dir1/file1.txt dir2/file1_copy.txt

● This copies file1.txt from dir1 to dir2 and renames it as file1_copy.txt.

c. Move file2.txt from dir1 to dir3


mv dir1/file2.txt dir3/

● This moves file2.txt from dir1 to dir3.

d. Remove file3.txt from dir1


rm dir1/file3.txt

● This deletes file3.txt from dir1.

3. Viewing and Editing Files

a. Use the cat command to concatenate file1.txt and file1_copy.txt


bash
Copy code
cat dir1/file1.txt dir2/file1_copy.txt

● This displays the contents of both file1.txt and file1_copy.txt in sequence.

b. Use the more command to view the contents of file1.txt


bash
Copy code
more dir1/file1.txt

● This displays the contents of file1.txt with pagination.

c. Use the less command to view the contents of file1_copy.txt


bash
Copy code
less dir2/file1_copy.txt

● This also allows you to view the contents of file1_copy.txt, but less is more
interactive than more.

d. Use the head command to display the first 10 lines of file1.txt


bash
Copy code
head dir1/file1.txt

● Displays the first 10 lines of file1.txt.

e. Use the tail command to display the last 10 lines of file1.txt


bash
Copy code
tail dir1/file1.txt

● Displays the last 10 lines of file1.txt.

4. Text Manipulation and Sorting

a. Use the echo command to append "Hello, World!" to file1.txt


bash
Copy code
echo "Hello, World!" >> dir1/file1.txt

● This appends the text "Hello, World!" to the end of file1.txt.

b. Use the printf command to add formatted text to file1.txt


bash
Copy code
printf "Date: %s\n" "$(date)" >> dir1/file1.txt

● This appends the current date to file1.txt in a formatted style.

c. Use the sort command to sort the contents of file1.txt and save the sorted output
to sorted_file1.txt
bash
Copy code
sort dir1/file1.txt > dir1/sorted_file1.txt

● This sorts the contents of file1.txt and saves it to sorted_file1.txt.

d. Use the uniq command to remove duplicate lines from sorted_file1.txt


bash
Copy code
uniq dir1/sorted_file1.txt > dir1/unique_sorted_file1.txt

● This removes duplicate lines from sorted_file1.txt and saves the result in
unique_sorted_file1.txt.

5. Cut, Paste, and Split Commands

a. Use the cut command to extract the first column from sorted_file1.txt and save it
to column1.txt
bash
Copy code
cut -d ' ' -f 1 dir1/sorted_file1.txt > dir1/column1.txt

● This extracts the first field (column) from sorted_file1.txt assuming space (' ') as
the delimiter and saves it to column1.txt.

b. Use the paste command to combine column1.txt and sorted_file1.txt into a


new file pasted_file.txt
bash
Copy code
paste dir1/column1.txt dir1/sorted_file1.txt > dir1/pasted_file.txt

● This merges the contents of column1.txt and sorted_file1.txt side by side and
saves the result in pasted_file.txt.

c. Use the split command to split file1.txt into pieces of 5 lines each
bash
Copy code
split -l 5 dir1/file1.txt dir1/file1_part_

● This splits file1.txt into smaller files, each containing 5 lines, with names like
file1_part_aa, file1_part_ab, etc.
6. User and System Information

a. Display the username of the current user


bash
Copy code
whoami

● This displays the username of the current user.

b. Display the system information


bash
Copy code
uname -a

● This displays detailed information about the system including the kernel name, version,
and architecture.

c. Display the current date and time


bash
Copy code
date

● This shows the current date and time.

d. Display the users currently logged in


bash
Copy code
who

● This lists the users currently logged in to the system.

e. Display detailed information about a user


bash
Copy code
finger <username>

● This provides detailed information about the specified user (e.g., <username> can be
replaced with a specific username).
7. Security by File Permissions

a. Create a file named permissions_test.txt


bash
Copy code
touch permissions_test.txt

● This creates an empty file named permissions_test.txt.

b. Change its permissions to read-only for the owner and no permissions for others
bash
Copy code
chmod 400 permissions_test.txt

● This sets the file permissions so that only the owner can read the file, and others have
no permissions.

c. Display the permissions of permissions_test.txt


bash
Copy code
ls -l permissions_test.txt

● This displays the file's permissions in the long format (e.g., -r--------).

d. Change the ownership of permissions_test.txt to a different user


bash
Copy code
sudo chown <newuser> permissions_test.txt

● This changes the ownership of the file to <newuser>. You need sudo privileges to
execute this command.

e. Change the group of permissions_test.txt to a different group


bash
Copy code
sudo chgrp <newgroup> permissions_test.txt
*This changes the group of the file to <newgroup>, and you need sudo permissions.

You might also like