The Touch Command

Posted: July 22nd, 2009 | Author: Troy | Filed under: Linux, Solaris | No Comments »

The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.

touch’s syntax is

touch [option] file_name(s)

When used without any options, touch creates new files for any file names that are provided as arguments (i.e., input data) if files with such names do not already exist. Touch can create any number of files simultaneously.

Thus, for example, the following command would create three new, empty files named file1, file2 and file3:

touch file1 file2 file3

A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move or rename files and directories), it does not automatically overwrite (i.e., erase the contents of) existing files with the same name. Rather, it merely changes the last access times for such files to the current time.

Several of touch’s options are specifically designed to allow the user to change the timestamps for files. For example, the -a option changes only the access time, while the -m option changes only the modification time. The use of both of these options together changes both the access and modification times to the current time, for example:

touch -am file3

The -r (i.e., reference) option followed directly by a space and then by a file name tells touch to use that file’s time stamps instead of current time. For example, the following would tell it to use the times of file4 for file5:

touch -r file4 file5

The -B option modifies the timestamps by going back the specified number of seconds, and the -F option modifies the time by going forward the specified number of seconds. For example, the following command would make file7 30 seconds older than file6.

touch -r file6 -B 30 file7

The -d and -t options allow the user to add a specific last access time. The former is followed by a string (i.e., sequence of characters) in the date, month, year, minute:second format, and the latter uses a [[CC]YY]MMDDhhmm[.ss] format. For example, to change the last access time of file8 to 10:22 a.m. May 1, 2005, 1 May 2005 10:22 would be enclosed in single quotes and used as follows, i.e.,:

touch -d '1 May 2005 10:22' file8

Partial date-time strings can be used. For example, only the date need be provided, as shown for file9 below (in which case the time is automatically set to 0:00):

touch -d '14 May' file9

Just providing the time, as shown below, automatically changes the date to the current date:

touch -d '14:24' file9

The most commonly used way to view the last modification date for files is to use the ls command with its -l option. For example, in the case of a file named file10 this would be

ls -l file10

The complete timestamps for any file or directory can be viewed by using the stat command. For example, the following would show the timestamps for a file named file11:

stat file11

The –help option displays a basic list of options, and the –version option returns the version of the currently installed touch program.


‘Find’ Command Examples

Posted: July 15th, 2009 | Author: Troy | Filed under: Linux | Tags: | No Comments »

The following examples illustrate typical uses of the command find for finding files on a computer.

find / -name game

Looks for a file named “game” starting at the root directory (searching all directories including mounted filesystems). The `-name’ option makes the search case sensitive. You can use the `-iname’ option to find something regardless of case.

find /home -user joe

Find every file under the directory /home owned by the user joe.

find /usr -name *stat

Find every file under the directory /usr ending in “stat”.

find /var/spool -mtime +60

Find every file under the directory /var/spool that was modified more than 60 days ago.

find /tmp -name core -type f -print | xargs /bin/rm -f

Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.

find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing single or double quotes, spaces or newlines are correctly handled. The -name test comes before the -type test in order to avoid having to call stat(2) on every file.

find . -type f -exec file ‘{}’ \;

Runs `file’ on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though ‘;’ could have been used in that case also.

find / \( -perm -4000 -fprintf /root/suid.txt ‘%#m %u %p\n’ \), \
\( -size +100M -fprintf /root/big.txt ‘%-10s %p\n’ \)

Traverse the filesystem just once, listing setuid files and directories into /root/suid.txt and large files into /root/big.txt.

find $HOME -mtime 0

Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

find . -perm 664

Search for files which have read and write permission for their owner, and group, but which other users can read but not write to. Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.

find . -perm -664

Search for files which have read and write permission for their owner and group, and which other users can read, without regard to the presence of any extra permission bits (for example the executable bit). This will match a file which has mode 0777, for example.

find . -perm /222

Search for files which are writable by somebody (their owner, or their group, or anybody else).

find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w

All three of these commands do the same thing, but the first one uses the octal representation of the file mode, and the other two use the symbolic form. These commands all search for files which are writable by either their owner or their group. The files don’t have to be writable by both the owner and group to be matched; either will do.

find . -perm -220
find . -perm -g+w,u+w

Both these commands do the same thing; search for files which are writable by both their owner and their group.

find . -perm -444 -perm /222 ! -perm /111
find . -perm -a+r -perm /a+w ! -perm /a+x

These two commands both search for files that are readable for everybody (-perm -444 or -perm -a+r), have at least on write bit set (-perm /222 or -perm /a+w) but are not executable for anybody (! -perm /111 and ! -perm /a+x respectively)


Zip Command

Posted: May 4th, 2009 | Author: Troy | Filed under: Linux | Tags: | No Comments »

The following examples illustrate typical uses of the command zip for packaging a set of files into an “archive” file. also called “zip file”. The command uses the standard zip file format. The archive files can therefore be used to tranfer files and directories between commonly used operating systems.

zip archivefile1 doc1 doc2 doc3 This command creates a file “archivefile1.zip” which contains a copy of the files doc1. doc2. and doc3. located in the current directory.

zip archivefile1 * This command creates a file “archivefile1.zip” which contains a copy of all files in the current directory in compressed form.

However. files whose name starts with a “.” are not included. The extension “.zip” is added by the program.

zip archivefile1 .* * This version includes the files that start with a dot. But subdirectories are still not included.

zip -r archivefile1 . This copies the current directory. including all subdirectories into the archive file.

zip -r archivefile2 papers This copies the directory “papers”. located in the current directory. into “archivefile2.zip”.

zip -r archivefile3 /home/joe/papers This copies the directory “/home/joe/papers” into “archivefile3.zip”. Since in this case the absolute path is given. it doesn’t matter what the current directory is. except that the zip file will be created there.
The command unzip extracts the files from the zip file.

unzip archivefile1.zip This writes the files extracted from “archivefile1.zip” to the current directory.


Mount Points

Posted: May 4th, 2009 | Author: Troy | Filed under: Linux | Tags: | No Comments »

Below are various commands for discovering. mounting and un-mounting drives.

Command:
fdisk -l
will give you the hd* (* = number) of the Hard Drive you want to format

To format the drive. as ROOT
enter this command
mkfs /dev/hd*

Line for the /etc/fstab file:
/dev/sda1 /mnt/sdb1 auto noauto.user.owner 0 0

Mount the drive:
mount -t auto /dev/sdb1 /mnt/dev_backups


Setting File Permissions

Posted: May 4th, 2009 | Author: Troy | Filed under: Linux, PHP | Tags: , | No Comments »

Here’s a few facts about locking down the server when it comes to hosting PHP scripts.

1) No PHP file should ever require the X bit (ie. you should be able to set all PHP files to 744 - rxwr–r–)
2) All directories do need X bit so set all directories to (755 - rxwr-xr-x)

The following steps should be performed to properly set the file permissions

- chown -R user /direcory/to/receive/new/permissions
- chgrp -R group /directory/to/receive/new/permissions
- chmod -R 744 /directory/to/receive/new/permissions - (this changes all files and directories to rwxr–r–. so still need to set all directories with the X bit)
- find /dir/to/chmod/all/dirs -type d -exec chmod 755 {} \;


Add Samba Users

Posted: May 4th, 2009 | Author: Troy | Filed under: Linux | Tags: | No Comments »

Pretty simple. First have to make sure that you have a system user with the same user name added first:
Good article for that: http://www.ahinc.com/linux101/users.htm

Then, in Red Hat type distros you’ll need the smbpasswd file which contains the samba user names and passwords.

To add a new samba user:
smbpasswd -a [username]

To delete a samba user:
smbpasswd -x [username]


Secure Copy (scp)

Posted: May 4th, 2009 | Author: Troy | Filed under: Linux | Tags: | No Comments »

scp stands for secure cp (copy). which means that you can copy files across an ssh connection that will be encrypted. and therefore secured.

You can this way copy files from or to a remote server. you can even copy files from one remote server to another remote server. without passing through your PC.

Usage: scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]

EXAMPLE:
scp /var/www/html/somedirectory/somezipfile.zip root@ip_address:/var/www/html/someotherdirectory
scp root@myserver:/var/www/html/somezipfile.zip root@ip_address:/var/www/html/someotherdirectory

Description of options

from-host
Is the name or IP of the host where the source file is. this can be omitted if the from-host is the host where you are actually issuing the command

user
Is the user which have the right to access the file and directory that is supposed to be copied in the cas of the from-host and the user who has the rights to write in the to-host

source-file
Is the file or files that are going to be copied to the destination host. it can be a directory but in that case you need to specify the -r option to copy the contents of the directory

destination-file
Is the name that the copied file is going to take in the to-host. if none is given all copied files are going to maintain its names

Options

-p  Preserves the modification and access times. as well as the permissions of the source-file in the destination-file
-q  Do not display the progress bar
-r  Recursive. so it copies the contents of the source-file (directory in this case) recursively
-v  Displays debugging messages

Examples

scp *.txt user@remote.server.com:/home/user/

This will copy all files with .txt extension to the directory /home/user in the remote.server.com host

scp -r miguel@10.1.2.2:/home/duder/ duder@10.1.2.3:/home/duder/

This is going to recursively copy all files from miguel’s Home directory on 10.1.2.2 host to his Home directory in 10.1.2.3 host.

Note

To use this command you need to have open-ssh installed in the hosts