Managing folders and files

Folders

Create folder

mkdir [foldername]

Remove folder

Only works if the folder is empty

rm [foldername]

Use -r (recursive) to remove the folder and its contents recursively.

rm -r [foldername]

Copy folder

Use -r (recursive) to include subdirectories.

cp -r [foldername] [newfoldername]

Move folder

mv [folder1] [folder1]

Change folder permissions

If you want to change folder permissions to read/write to owner and group do:

find . -type d -exec chmod 775 {} \;

Files

Create file

touch <filename>;

Change file permissions

If you want to change file permissions to read/write to owner and group do:

find . -type f -exec chmod 664 {} \;

Renaming files

For example for renaming all images with format .jpeg to .jpg on all the subfolders of the path /my_images, you can use the following command:

find /my_images -type f -name '*.jpeg' -exec bash -c 'mv "$0" "${0%%.jpeg}.jpg"' {} \;

A brief explanation:

  • -type f is for searching files only.
  • “$0” adds the name of the image found to the mv command.
  • “${0%%.jpeg}.jpg” takes the name of the image found, deletes .jpeg from it and adds .jpg.
  • {} Injects the current file name being processed.
  • \; Token for closing the exec function.

If you want to know which operations will be executed you can just add echo before the command mv, it will print on screen all operations without actually performing any change:

find /my_images -type f -name '*.jpeg' -exec bash -c 'echo mv "$0" "${0%%.jpeg}.jpg"' {} \;

Move/Rename files

For moving or rename a file just use the mv command:

mv  [filename] [new filename]

Miscellaneous

Searching folders/files

Using the command “find“.
The first “/” means: Search in all folders.
-name/-iname means: Case sensitive/insensitive.
-type d means: Type directory (use “f” for files).

find / -name [filename] -type d

Other way is using “locate” (faster than find). It scans a database for files with a given name, and print their names.

locate [filename]

Changing folder/file permissions

Use -R (recursive) to change files and directories recursively.

chmod -R 0755 [foldername]

List folders/files

Use -la for showing all files in a long listing format (showing folder/file permissions)

ls -la

File monitoring

tail has a special command line option -f (follow) that allows a file to be monitored. Instead of just displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages:

tail -f /var/adm/messages

In cases when the user is following a log file that rotates then it is advisable to use the -F option as it keeps following the log even when it is recreated, renamed, or removed as part of log rotation.

tail -F /var/adm/messages

Leave a Reply

Your email address will not be published. Required fields are marked *