Библиотека сайта rus-linux.net
7.2. Working with files and folders
- mkdir
Make a directory. Use mkdir -p to create subdirectories automatically.
Directories are Folders Directories are sometimes called folders in other operating systems (such as Microsoft Windows)
Examples:
mkdir -p /home/matt/work/maths
This would create the directories “work” and “maths” under matt's home directory (if matt's home directory didn't exist it would create that too).
mkdir foo
This would create a directory in the current path named “foo”.
- rm
Remove/delete a file(s) or directories(s). You can use standard wildcards with this command Section 20.4.1.
Command syntax:
rm -options file_or_folder
You can of course use standard wildcards to delete multiple files or multiple directories and files.
Use the -R or -r option to remove recursively, this removes everything within subdirectories. Also try the -f option to force removal (useful when you don't want to be prompted).
Disabling Aliases (per execution) On some systems such as Mandrake an alias will send rm to rm -i (prompting you for every file you wish to delete). To override this use: \rm -R directory (using the \ disables the alias for this run only)
- rmdir
Remove an empty directory. If you want to remove a directory with files in it type “ rm -R directory”, read above for information on rm -R
Command syntax:
rmdir directory
This will only remove directory if it's empty otherwise it will exit with an error message.
- mv
Move a file or a directory to a new location or rename a file/directory.
Rename example:
mv filename1 filename2
Renames filename1 to filename2.
To move a file or directory, simply type:
mv original_file_or_folder new_location
Note that this command can use standard wildcards Section 20.4.1 to move files (not for renaming).
Move and rename Note that you can also move and rename a file in a single command. The difference is with the destination (right hand side) you change the filename to the new name of the file.
For example typing:
mv /etc/configuration.txt /home/joe/backupconfig
This would move the file "configuration.txt" to /home/joe/ and rename it "backupconfig"
- cp
Copy a file. Has a number of useful options, such as -R (or -r) which recursively copies directories and subdirectories.
Command syntax:
cp -options file_or_files new_location
Examples:
cp file1 file2
Simply copy file1 to file2 (in the same directory).
cp /tmp/file1 ~/file2 /mnt/win_c
Where the last option is the directory to be copied to. The above example copies two files from different areas of the file system to /mnt/win_c
cp -R directory_and_or_files new_location
This command will copy directories (and all subdirectories) and/or files to new_location
Note that this command can use standard wildcards Section 20.4.1 to copy multiple files.
You may also like to try the “-u” when moving large directories around, this copies only if the source file is newer than the destination to where you are copying to, or if the destination file does not exist at all.
- ln
Create a link to a file. There are two types of links:
- Hard═links
Hard links are considered pointers to a file (the number is listed by typing ls -l). Each hard-link is a reference to a file.
The file itself only goes away when all hard-links are deleted. If you delete the original file and there are hard links to it the original file will remain.
Example:
ln target_name link_name
Will create a “hard link” to target_name called link_name, you need to delete both of these to remove the file.
- Symbolic═links
Symbolic links are created by typing “ln -s”. When you remove the original file the symbolic link becomes broken, a symbolic link is similar to a windows “short-cut”.
The advantage of symbolic links is that the target can be to something on another file-system, while hard-links can only exist on the same file-system.
For example:
ln -s target_name link_name
This creates a symbolic link to “target_name” called “link_name”, if you delete the original file the symbolic link won't work (it becomes a broken link).
- shred
Securely remove a file by overwriting it first. Prevents the data from being recovered by software (and even by most hardware), please be very careful when using shred as you may never be able to retrieve the data you have run the application on.
For example:
shred -n 2 -z -v /dev/hda1
“What this tells shred, is to overwrite the partition 2 times with random data (- n 2) then finish it up by writing over it with zeroes (-z) and show you its progress (-v). Of course, change /dev/hda1 to the correct partition . Each pass can take some time, which is why I set it to only do 2 random passes instead of the default 25. You can adjust this number, of course, to your particular level of paranoia and the amount of time you have.
Since shred writes on such a low-level, it doesn't actually matter what kind of filesystem is on the partition--everything will be unrecoverable. Once shred is finished, you can shutdown the machine and sell or throw away the drive with peace of mind.
...However, even shre dding devices is not always completely reliable. For example, most disks map out bad sectors invisibly to the application; if the bad sectors contain sensitive data, `shred' won't be able to destroy it. [ shred info page ].”[1]
Shredding files doesn't work with all filesystems Please note that as mentioned in the shred manual page (please see the manual and preferably info pages for more information). shred does not work correctly on log-structured or journaled filesystems, such as JFS, ReiserFS, XFS, Ext3 and many other modern filesystems
Alternatives to using shred shred has its disadvantages when run on a filesystem. First of all since it has to be installed you cannot run shred on your operating systems filesystem, you also cannot use shred on a windows machine easily since you cannot install shred on this machine.
You may like to try alternatives such as the DBAN project that create self-booting floppy disks that can completely erase a machines hard disk.
You may also like to see how chattr can assist you in shredding files once they are removed (it has similar problems to shred, only ext2 and ext3 style filesystems...), please see Section 14.2.
- du
Displays information about file size. Use du filename to display the size of a particular file. If you use it on directories it will display the information on the size of the files in the directory and each subdirectory.
Options for du (use du -option(s)):
-c -- this will make du print a grand total after all arguments have being processed.
-s -- summarises for each argument (prints the total).
-h -- prints things in “ human readable” mode; for example printing 1M (megabyte) rather than 1,024,000 (bytes).
Using the -hs options on a directory will display the total size of the directory and all subdirectories.
Command syntax:
du -options file_directory_or_files
Example:
du -hs *
This command will list the size of all files in the current directory and it will list the size of subdirectories, it will list things in human-readable sizes using 1024 Kb is a Megabyte, M for megabyte, K for kilobyte etc.
- file
Attempts to find out what type of file it is, for example it may say it's: binary, an image file (well it will say jpeg, bmp et cetera), ASCII text, C header file and many other kinds of files, it's a very useful utility.
Command syntax:
file file_name
- stat
Tells you detailed information about a file, including inode number creation/access date. Also has many advanced options and uses.
For simple use type:
stat file
- dd
Copies data on a very low level and can be used to create copies of disks Section 20.3 and many other things (for example CD image files).
dd can also perform conversions on files and vary the block size used when writing the file.
Command syntax, note the block size and count are optional and you can use files instead of devices...
Please note dd is an advanced and difficult to use command. Its also very powerful, so be careful what you do with it
Command syntax:
dd if=/dev/xxx of=/dev/xxx bs=xxxx count=x
Warning The command dd is used to work on a very low level. It can be used to overwrite important information such as your master-boot record or various important sections of your hard-disk. Please be careful when using it (especially when working with devices instead of files).
- touch
This command is used to create empty files, simply do touch file_name. It is also used to update the timestamps on files.
touch can be used to change the time and/or date of a file:
touch -t 05070915 my_report.txt[2]
This command would change the timestamp on my_report.txt so that it would look like you created it at 9:15. The first four digits stand for May 7th (0507), in MM-DD (American style), and the last four (0915) the time, 9:15 in the morning.
Instead of using plain numbers to change the time, you can use options similar to that of the date tool. For example:
touch -d '5 May 2000' some_file.txt
You can also use --date= instead of -d. Also have a look at the date command under Section 8.1 for examples on using -d and --date= (the syntax for the date part is exactly the same when using -d or --date).
- split
Splits files into several smaller files.
Use the -b═xx option to split into xx bytes, also try -k for kilobytes, and -m for megabytes. You can use it to split text files and any other files... you can use cat to re-combine the files.
This may be useful if you have to transfer something to floppy disks or you wish to divide text files into certain sizes.
Command syntax:
split -options file
This will split the input file into 1000 lines of input each (thats the default...), and output (using the above example), with the input name file, “fileaa” (1st part of file), “fileab” (2nd part of file), “fileac” (3rd part of file) etc. until the there is no more of the file left to split.
Notes
[1] | This information (as quoted) has come from the “Please, For the Love of All That's Recoverable, Shred Your Hard Drive!” article, number 18 in the Bibliography |
[2] | This particular command and explanation has been used (with editing) from the Linux Online Classroom, see [4] in the Bibliography for further information. |