0% found this document useful (0 votes)
619 views

Assignment 1

This document provides examples of Linux commands to perform various file operations within a folder: 1. The ls -R command lists all files inside subfolders within a folder. 2. The grep command searches for a string within all files in a folder. 3. A for loop renames all files within a folder with a suffix. 4. Another for loop renames files within a folder using the first word of the file content. 5. The wc command counts the total lines of all text files within a folder. 6. A for loop renames files within a folder by replacing a substring. 7. The history, sort and cut commands show the most commonly used commands

Uploaded by

Poongkodi Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
619 views

Assignment 1

This document provides examples of Linux commands to perform various file operations within a folder: 1. The ls -R command lists all files inside subfolders within a folder. 2. The grep command searches for a string within all files in a folder. 3. A for loop renames all files within a folder with a suffix. 4. Another for loop renames files within a folder using the first word of the file content. 5. The wc command counts the total lines of all text files within a folder. 6. A for loop renames files within a folder by replacing a substring. 7. The history, sort and cut commands show the most commonly used commands

Uploaded by

Poongkodi Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

c  




  

Write a command to list all the files inside a folder i.e. if there is a folder inside a folder then it
should list all files inside the sub-folder which is inside the folder to be listed.
  : ls -R

  
Search all the files which contains a particular string, say ³include´ within a folder.
 grep include ./*

  
Rename all the files within a folder with suffix ³Unix_´ i.e. suppose a folder has two files a.txt
and b.pdf than they both should be renamed from a single command to Unix_a.txt and
Unix_b.pdf
 for f in *.*; do mv ³$f´ Unix_´$f´; done

  
Rename all files within a folder with the first word of their content(remember all the files should
be text files. For example if a.txt contains ³Unix is an OS´ in its first line then a.txt should be
renamed to Unix.txt
 for f in *.txt; do d="$(head -1 "$f") ;x=´$(cut -d " " -f1 $f)´ ; mv $f $x; done

  
Suppose you have a C project in a folder called ³project´, it contains .c and .h files, it also
contains some other .txt files and .pdf files. Write a Linux command that will count the number
of lines of your text files. That means total line count of every file. (remember you have to count
the lines in .txt files only)
 wc ±l *.txt

  
Rename all files which contain the sub-string 'foo', replacing it with 'bar' within a given folder.
 for i in ./*foo*;do mv -- "$i" "${i//foo/bar}";done

  
Show the most commonly used commands from ³history´. [hint: remember the history
command, use cut, and sort it]
  history|sort|cut ±d-10 -n

You might also like