Wednesday, May 30, 2012

Find Files Modified After Specific Time

If you have GNU find, then there are a legion of relevant options. The only snag is that the interface to them is less than stellar:

-mmin n (modification time in minutes)
-mtime n (modification time in days)
-newer file (modification time newer than modification time of file)
-daystart (adjust start time from current time to start of day)
Plus alternatives for access time and 'change' or 'create' time.
The hard part is determining the number of minutes since a time.

One option worth considering: use touch to create a file with the required modification time stamp; then use find with -newer.

touch -t 200901031231.43 /tmp/wotsit
find . -newer /tmp/wotsit -print
rm -f /tmp/wotsit

This looks for files newer than 2009-01-03T12:31:43. Clearly, in a script, /tmp/wotsit would be a name with the PID or other value to make it unique; and there'd be a trap to ensure it gets removed even if the user interrupts, and so on and so forth.

Below is an example of how to move files that are older than 30 days :

find /storage/current/dbdumps/ -type f -mtime +30 -print | xargs -I {} mv {} /storage/archive/dbdumps

Wednesday, May 2, 2012

Beginner's Linux Commands

pwd - shows path to the current location. For more information, click the command.
cd - changes current directory to the one specified with the command. For example, cd temp will take you from the current directory to temp directory and cd .. will take you back to the parent directory.
man is a very helpful utility that displays help on pretty much any linux command.
Syntax: man ls will display help on using the listing - ls command. To get out of man, type q (quit)
ls - listing - outputs list of files and directories at the specified path and of specified pattern, such as: ls a*.sh will list all files that begin with "a" and end with ".sh". One of the most frequently used options of the ls command is -l. When used with ls, the output shows permissions on the file, such as "-rwxrw-r--", and some other information.
For more information on use of ls click on it.
cp - copy - copies files from one location to another.
Syntax: cp source destination, e.g. cp file1 myfiles/file1
For more information on use of cp click on it.
mv - move - moves and/or renames files. Unlike moving a file within windows folder structure, linux mv command can also be used as "rename" or "move and rename" equivalent.
For more information on use of mv click on it.
diff - find differences between two files - compares contents of two files and either returns a message if the files are different or displays the actual differences - depending on the options specified. There are many options that can be specified with this command.

Example:
-bash-3.2$ diff --brief <(sort file1) <(sort file2)
Files /dev/fd/63 and /dev/fd/62 differ
-bash-3.2$

For more information on how to use diff, click on it.
chmod - change mode - changes permissions for groups, owner, and others for a specific file. For example, to give access to file myfile.sh, type chmod 777 myfile.sh. Once this command finishes running, everybody will be allowed to edit and execute myfile.sh.
For more information, click on the command.
fgrep - searches contents of one file or multiple files for a specific pattern. For example, to search all files in the current folder for the word 'hello', type this: fgrep 'hello' *. This command will find all instances of word 'hello' in all the files within the current directory, but it will skip 'Hello', 'HELLO', and any other variation of it that has capitalized letters. To capture all of those instances, type the following: fgrep -i 'hello' *.
vi - opens the vi editor. To create a new file, type: vi newfilename; you can also just open the vi editor without specifying a new filename; this can be done when saving the new file: w newfilename. To edit an existing file, type vi existingfilename. For vi commands, see my post from april 2012.
cat - short for concatenate. Click the command to see more info on cat command.
echo - same as in DOS - outputs the text to the terminal. For example, echo "Hello World" will output the string Hello World.

Friday, April 27, 2012

Most Useful VI Commands

Once you start working in VI (pronounced "vee-eye"), there is no pointing with a mouse anymore. Instead, you need to learn to use the keyboard.


Below are some vi commands that I found useful when working with files that span hundreds if not thousands of lines of code:

To go to the very first line, type: :1 and hit "enter"
To display line numbers, type: :set number and hit "enter"
To replace all occurrencies of string X with string Y, type: :%s/X/Y/g and hit "enter"; don't forget to substitute the X and Y with actual strings and escape all the special characters if they are part of either of those strings.
If you only want to replace string X with string Y within a specific block of code, this is how you do it: mns/X/Y/g and hit "enter"; m and n are the beginning line number and the end of block line number.
To search for a specific string, type: /your search string goes here
This is case-sensitive
To make your search case insensitive, type: :set ic
To search backward, type: ?your search string goes here
To repeat the search forward, type: :n
To repeat the search backward, type: :N
To delete a block of text in vi, type mx at the beginning of the block and type d`x at the end of the block.
Also, to switch from command mode to input mode, type i
To switch from input mode to command mode, hit the Esc key

Happy coding!