GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  Subscribe our youtube channel
+91-7976731765 Free Online Tutorials || AI || ML || NLP ||  OpenCV || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
0 like 0 dislike
29 views
in Tutorial & Interview questions by Goeduhub's Expert (8.1k points)

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.1k points)
 
Best answer

A common and task of someone using the Linux Command Line (shell) is to search for files/directories with a certain name or containing certain text. There are 2 commands you should familiarise yourself with in order to accomplish this: 

Find files by name

find /var/www -name '*.css' 

This will print out the full path/filename to all files under /var/www that end in .css. Example output: 

/var/www/html/text-cursor.css 

/var/www/html/style.css For more info: man find

For more info:

 man find

Find files containing text 

grep font /var/www/html/style.css

This will print all lines containing the pattern font in the specified file. Example output: 

font-weight: bold; 

font-family: monospace; 

Another example: 

grep font /var/www/html/

This doesn't work as you'd hoped. You get:

 grep: /var/www/html/: Is a directory 

You need to grep recursively to make it work, using the -R option:

 grep -R font /var/www/html/ 

Hey nice! Check out the output of this one:

 /var/www/html/admin/index.php: echo '<font color=red><b> Error: no dice</b></font><br/>';
/var/www/html/admin/index.php: echo '<font color=red><b> Error: try again</b></font><br/>';

 /var/www/html/style.css: font-weight: bold; 

/var/www/html/style.css: font-family: monospace; 

Notice that when grep is matching multiple files, it prefixes the matched lines with the filenames. You can use the - h option to get rid of that, if you want.

 For more info: 

man grep 

 File Manipulation-

Files and directories (another name for folders) are at the heart of Linux, so being able to create, view, move, and delete them from the command line is very important and quite powerful. These file manipulation commands allow you to perform the same tasks that a graphical file explorer would perform.
Create an empty text file called myFile:
 touch myFile 
Rename myFile to myFirstFile:
 mv myFile myFirstFile 
View the contents of a file: 
cat myFirstFile 
View the content of a file with pager (one screenful at a time): 
less myFirstFile 
View the first several lines of a file:
 head myFirstFile 
View the last several lines of a file
tail myFirstFile 
Edit a file:
vi myFirstFile
See what files are in your current working directory: 
ls 
Create an empty directory called myFirstDirectory:
 mkdir myFirstDirectory 
Create multi path directory: (creates two directories, src and myFirstDirectory)
 mkdir -p src/myFirstDirectory 
Move the file into the directory: 
mv myFirstFile myFirstDirectory/
You can also rename the file: 
user@linux-computer:~$ mv myFirstFile secondFileName 
Change the current working directory to myFirstDirectory: 
cd myFirstDirectory
Delete a file: 
rm myFirstFile 
Move into the parent directory (which is represented as ..): 
cd .. 
Delete an empty directory: 
rmdir myFirstDirectory 
Delete a non-empty directory (i.e. contains files and/or other directories):
 rm -rf myFirstDirectory

 Make note that when deleting directories, that you delete ./ not / that will wipe your whole filesystem. 


File/Directory details

The ls command has several options that can be used together to show more information.
Details/Rights
The l option shows the file permissions, size, and last modified date. So if the root directory contained a dir called test and a file someFile the command:
user@linux-computer:~$ ls -l

Would output something like

-rw-r--r-- 1 user users 70 Jul 22 13:36 someFile.txt 

drwxrwxrwx 2 user users 4096 Jul 21 07:18 test  

The permissions are in format of drwxrwxrwx. The first character represents the file type d if it's a directory - otherwise. The next three rwx are the permissions the user has over the file, the next three are the permissions the group has over the file, and the last three are the permissions everyone else has over the file. 

The r of rwx stands for if a file can be read, the w represents if the file can be modified, and the x stands for if the file can be executed. If any permission isn't granted a - will be in place of r, w, or x. 

So from above user can read and modify someFile.txt but the group has only read-only rights.

 To change rights you can use the chmod ### fileName command if you have sudo rights. r is represented by a value of 4, w is represented by 2, and x is represented by a 1. So if only you want to be able to modify the contents to the test directory  

Owner rwx = 4+2+1 = 7

 Group r-x = 4+0+1 = 5

 Other r-x = 4+0+1 = 5

So the whole command is 

chmod 755 test  

Now doing a ls -l would show something like

drwxr-xr-x 2 user users 4096 Jul 21 07:20 test 

Readable Size 

Used in conjunction with the l option the h option shows file sizes that are human readable. Running
user@linux-computer:~$ ls -lh 

Would output: 

 total 4166

 -rw-r--r-- 1 user users 70 Jul 22 13:36 someFile.txt 

drwxrwxrwx 2 user users 4.0K Jul 21 07:18 test  

Hidden  

To view hidden files use the a option. For example 
user@linux-computer:~$ ls -a

Might list

.profile 

someFile.txt 

test 

Total Directory Size  

To view the size of the current directory use the s option (the h option can also be used to make the size more readable). 

user@linux-computer:~$ ls -s 

Outputs

 total 4166

 someFile.txt test

Recursive View  

Lets say test directory had a file anotherFile and you wanted to see it from the root folder, you could use the R option which would list the recursive tree. 

user@linux-computer:~$ ls -R 

Outputs

.: 

someFile.txt test

 ./test: 

anotherFile

 

Related questions

0 like 0 dislike
1 answer 38 views
0 like 0 dislike
1 answer 25 views
asked Nov 2, 2019 in Tutorial & Interview questions by Goeduhub Goeduhub's Expert (8.1k points)
0 like 0 dislike
1 answer 15 views
asked Nov 2, 2019 in Tutorial & Interview questions by Goeduhub Goeduhub's Expert (8.1k points)
0 like 0 dislike
1 answer 25 views
0 like 0 dislike
1 answer 57 views

 Goeduhub:

About Us | Contact Us  © goeduhub.com Social::   |  | 
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
...