background preloader

Tips, astuces, cas concrets

Facebook Twitter

3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions. Sometimes you get into a situation where you need to delete all files in a directory or simply cleanup a directory by removing all files except files of a given type (ending with a particular extension).

3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions

In this article, we will show you how to delete files in a directory except certain file extensions or types using rm, find and globignore commands. Before we move any further, let us start by briefly having a look at one important concept in Linux – filename pattern matching, which will enable us to deal with our issue at hand. Suggested Read: Use find Command to Search Multiple Filenames/Extensions in Linux In Linux, a shell pattern is a string that consists of the following special characters, which are referred to as wildcards or metacharacters: * – matches zero or more characters? There are three possible methods we shall explore here, and these include: Delete Files Using Extended Pattern Matching Operators *(pattern-list) – matches zero or more occurrences of the specified patterns? How to Extract Tar Files to Specific or Different Directory in Linux.

The tar utility is one of the utilities that you can use to create a backup on a Linux system.

How to Extract Tar Files to Specific or Different Directory in Linux

It includes many options that one can use to specify the task to achieve. One thing to understand is that you can extract tar files to a different or specific directory, not necessarily the current working directory. You can read more about tar backup utility with many different examples in the following article, before proceeding further with this article. Mastering tar Command with this 18 Examples in Linux In this guide, we shall take a look at how to extract tar files to a specific or different directory, where you want the files to reside.

The general syntax of tar utility for extracting files: Data Recovery Techniques on Linux. When one of my friends called telling me that he had accidentally deleted some important files from his drive, his exasperation was understandable.

Data Recovery Techniques on Linux

It happens to everyone at some point of their computer-using lives. Unfortunately, in his case, those were some extremely important documents that, had he not recovered them, could have proven very costly. Fortunately, I managed to guide him through the process of recovery, and now, I'm going to guide you too. What happens when a file is deleted? Data stored in files has a table of contents which indicates the storage location for each file on the drive. Removal/Deletion of a file leads to the removal of only of the table of contents. First, let's go through a few Dos and Don'ts .

DO's and Don'ts when a file is lost Don'ts: Do not write any data on the device. Do's: Shutdown the system immediately. There are various tools which can be used for data-recovery. Here are some trusted tools that can be used: Extundelete PhotoRec. Scripting made fun. I have never been a fan of programming or scripting.

Scripting made fun

Thought is was a skill I was unable to learn. When learning to administer a Linux system, scripting can not be avoided. One of the skills every sysAdmin must learn is scripting. The benefit of scripting is to automate a task or job that is constantly run every time a system is running. A sysAdmin can make that job run on its own and concentrate on other tasks that are not so easily automated.

I was not automating anything, just getting used to writing scripts. There are default variables already set on a Linux system. This is a screen shot of the default variables on my Linux Mint 17 system. XDG_SESSION_ID=c1 TERM=xterm SHELL=/bin/bash XDG_SESSION_COOKIE=9f6e6efa3c8a79091dbec7c85403e053-1410643266.227255-1746036259 GNOME_KEYRING_CONTROL=/run/user/1000/keyring-ccaIQh USER=x OPENDVD=eject /dev/sr0.

Linux Blog » rmnl — remove new line characters with tr, awk, perl, sed or c/c++ How to remove new lines from files or pipe streams under Linux?

Linux Blog » rmnl — remove new line characters with tr, awk, perl, sed or c/c++

This post contains simple examples that show how to use common Linux shell tools such as tr, awk/gawk, perl, sed and many others to delete new line characters. C and C++ source codes are also provided. They can be compiled into a binary tool that removes new lines. To get started, here is an example text file: days.txt. Lets have a look at its content by running the following command from shell. cat days.txt ©2007-2008 dsplabs.com.au The output is shown below. Mon Tue Wed Thu Fri Sat Sun If the new lines were removed from days.txt then its content would look like this: MonTueWedThuFriSatSun If instead of simply removing the new line characters they were replaced with spaces then the output would look as follows.

Below are simple examples of two types: one to remove new lines and the other to replace them with spaces. Tr — remove new line characters with tr tr -d '\n' < days.txt cat days.txt | tr -d '\n' sed ':a;N;$!