How to Delete Files and Folders via SSH

Sometimes you would need to remove a file or a folder from the system. To do so using SSH, you would need to execute the appropriate command – rm.

The command in its simplest form looks like:

However listing all files/folders that need to be deleted can be quite time consuming. Fortunately, rm accepts several arguments which can ease your task. In the above example, you could type:

This will match all files starting with ‘myFile’ and ending in ‘.txt’ and delete them.

To delete a whole folder and its content recursively, you can use:

To delete all files/folders in the current directory, without deleting the directory itself, you would need to use:


How to Extract and Create Archives via SSH

Sometimes you would need to extract or create an archive file, i.e to install a script, you would usually download an archive and extract it to continue the installation. The very first step in the process would be to identify the exact archive type by looking at the file extension. The most common archive types are zip (ending with .zip), tar (.tar), Tar+Gunzip (.tar.gz), Bzip (.bz2) and Rar (.rar).

Each archive type has its own command for compressing/extracting as listed below.

To extract a ZIP file, you can use:

To extract a Tar file, you can use:

To extract a Tar.Gz file, you can use:

To extract a Rar file, you can use:

Each archive type has its own mechanism to create a new archive file. The most commonly used however is the tar.gz format. An example of creating such a file is:

The above will archive the entire folder foldername in an archive named archive-name.tar.gz in the current working directory.

(Visited 13 times, 1 visits today)