commands

All posts tagged commands

Had an issue that wanted to translate two digit years into 4 digits… I realize this would be little bit different if we needed this century but we didn’t just needed 1900’s.

Two digits were getting written as 35-37 or 40-50.  Which needed to be translated to 1935 1936 1937 or 1940.1941 1942 1943 1944 1945 1946 1947 1948 1949 1950.

Or

By creating a file named ‘years’ with two digit years.  I’m sure there are better ways then this ….

67 80

55 60

45 50

 

 

(pulled from http://www.folkstalk.com/2011/12/101-examples-of-using-find-command-in.html)

Find Command in Unix and Linux Examples

Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory hierarchy. The syntax of find command is

Let see some practical exercises on using find command.

1. How to run the last executed find command?

This will execute the last find command. It also displays the last find command executed along with the result on the terminal.

2. How to find for a file using name?

This will find all the files with name “sum.java” in the current directory and sub-directories.

3. How to find for files using name and ignoring case?

This will find all the files with name “sum.java” while ignoring the case in the current directory and sub-directories.

4. How to find for a file in the current directory only?

This will find for the file “sum.java” in the current directory only

5. How to find for files containing a specific word in its name?

It displayed all the files which have the word “java” in the filename

6. How to find for files in a specific directory?

This will look for the files in the /etc directory with “java” in the filename

7. How to find the files whose name are not “sum.java”?

This is like inverting the match. It prints all the files except the given file “sum.java”.

8. How to limit the file searches to specific directories?

You can see here the find command displayed all the files with name “sum.java” in the current directory and sub-directories.

a. How to print the files in the current directory and one level down to the current directory?

b. How to print the files in the current directory and two levels down to the current directory?

c. How to print the files in the subdirectories between level 1 and 4?

9. How to find the empty files in a directory?

10. How to find the largest file in the current directory and sub directories

The find command “find . -type f -exec ls -s {} \;” will list all the files along with the size of the file. Then the sort command will sort the files based on the size. The head command will pick only the first line from the output of sort.

11. How to find the smallest file in the current directory and sub directories

Another method using find is

12. How to find files based on the file type?

a. Finding socket files

b. Finding directories

c. Finding hidden directories

d. Finding regular files

e. Finding hidden files

13. How to find files based on the size?

a. Finding files whose size is exactly 10M

b. Finding files larger than 10M size

c. Finding files smaller than 10M size

14. How to find the files which are modified after the modification of a give file.

This will display all the files which are modified after the file “sum.java”

15. Display the files which are accessed after the modification of a give file.

16. Display the files which are changed after the modification of a give file.

17. How to find the files based on the file permissions?

This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command “ls -l”.

18. Find the files which are modified within 30 minutes.

19. Find the files which are modified within 1 day.

20. How to find the files which are modified 30 minutes back

21. How to find the files which are modified 1 day back.

22. Print the files which are accessed within 1 hour.

23. Print the files which are accessed within 1 day.

24. Display the files which are changed within 2 hours.

25. Display the files which are changed within 2 days.

26. How to find the files which are created between two files.

So far we have just find the files and displayed on the terminal. Now we will see how to perform some operations on the files.

27.  How to find the permissions of the files which contain the name “java”?

Alternate method is

28.  Find the files which have the name “java” in it and then display only the files which have “class” word in them?

29.  How to remove files which contain the name “java”.