files – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Mon, 22 Aug 2022 13:25:32 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 What is the /etc/nsswitch.conf file in Linux https://tecadmin.net/what-is-etc-nsswitch-conf-file/ https://tecadmin.net/what-is-etc-nsswitch-conf-file/#respond Tue, 28 Jun 2022 01:47:07 +0000 https://tecadmin.net/?p=30387 What is /etc/nsswitch.conf? /etc/nsswitch.conf is a Linux configuration file that specifies how the system should switch between different name service providers. The file can be used to configure which services should be used for hostname lookup, password lookups, and so on. “/etc/nsswitch.conf” file is read by the Name Service Switch (NSS) library when the system [...]

The post What is the /etc/nsswitch.conf file in Linux appeared first on TecAdmin.

]]>
What is /etc/nsswitch.conf?

/etc/nsswitch.conf is a Linux configuration file that specifies how the system should switch between different name service providers. The file can be used to configure which services should be used for hostname lookup, password lookups, and so on.

“/etc/nsswitch.conf” file is read by the Name Service Switch (NSS) library when the system starts up. The NSS library then uses the information in “/etc/nsswitch.conf” to determine which name service providers should be used for each type of lookup.

“/etc/nsswitch.conf” is a critical part of the Linux operating system, and any changes to the file can potentially cause serious problems. As such, it is important to understand how “/etc/nsswitch.conf” works before making any changes to the file.

You can view the content of the “/etc/nsswitch.conf” file using the following command.

cat /etc/nsswitch.conf 
Output:
# # Example configuration of GNU Name Service Switch functionality. # If you have the `glibc-doc-reference' and `info' packages installed, try: # `info libc "Name Service Switch"' for information about this file. passwd: files systemd group: files systemd shadow: files gshadow: files hosts: files dns networks: files protocols: db files services: db files ethers: db files rpc: db files netgroup: nis

Understand the use of /etc/nsswitch.conf with an example

Let’s understand the uses of /etc/nsswitch.conf with an example. In this file, you will find an entry like the below:

hosts:          files dns

The above entry tells the order to resolving any domain name. First, the system will check domain mapping in files (/etc/hosts), If a matching entry is found it will use it, else the system will check with the DNS servers.

Any domain resolve request will go to the DNS server, only if no matching entry is found in the /etc/hosts file.

The post What is the /etc/nsswitch.conf file in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/what-is-etc-nsswitch-conf-file/feed/ 0
How to Search Recently Modified Files in Linux https://tecadmin.net/how-to-search-recently-modified-files-in-linux/ https://tecadmin.net/how-to-search-recently-modified-files-in-linux/#respond Wed, 16 Feb 2022 09:24:53 +0000 https://tecadmin.net/?p=28575 This tutorial will help you to find recently modified files in Linux via command line . The find command allows us to define duration in Minutes or Days. The minutes are define with -mmin and the days value can be defined with -mtime You can also define the search criteria to find files modified within [...]

The post How to Search Recently Modified Files in Linux appeared first on TecAdmin.

]]>
This tutorial will help you to find recently modified files in Linux via command line .

The find command allows us to define duration in Minutes or Days. The minutes are define with -mmin and the days value can be defined with -mtime

You can also define the search criteria to find files modified within or before specified duration. For example, to search files modified before, use “+” (positive) with duration (eg: +1, +24 etc). To search files modified within duration use “-” (negative) sign with duration value (eg: -1, -24) etc.

Find All Modified Files Less Than Time

  1. Modified within 10 Minutes:- Search all files modified within 10 minutes in the current directory. Use -mmin -10 means the files last modified less than 10 minutes.
    find . -type f -mmin -10 
    
  2. Modified within 2 Hours:- Find all files modified within 2 hours in the current directory. Use -mmin -120 means the files last modified less than 120 minutes ie equal to 2 hours.
    find . -type f -mmin -120 
    
  3. Modified within 1 day:- Search all files modified within 24 hours in the current directory. To define range in days use -mtime. For example -mtime -1 means the files last modified les than 24 hours ago.
    find . -type f -mtime -1 
    

Find All Modified Files Before Time

The above example, find all file modified within specified duration. But you can also search files modified before specified duration with the help of below examples.

  1. Modified older than 10 Minutes:- Search all files modified before 10 minutes in the current directory. Use -mmin +10 option, which means find all files modified more than 10 minutes ago.
    find . -type f -mmin +10 
    
  2. Modified older than 2 Hours:- Find all files modified before 2 hours in the current directory. Use -mmin +120 options to search file modified older than 120 minutes (ie 2 hours).
    find . -type f -mmin +120 
    
  3. Modified older than 1 day:- Search all files modified more than 24 hours ago in the current directory. You can use -mtime option to define duration in days. For example -mtime +1 means find all files modified before 24 hours.
    find . -type f -mtime +1 
    

The post How to Search Recently Modified Files in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-search-recently-modified-files-in-linux/feed/ 0
Handling filenames with spaces in Linux https://tecadmin.net/reference-filenams-with-spaces-in-linux/ https://tecadmin.net/reference-filenams-with-spaces-in-linux/#respond Wed, 15 Sep 2021 05:30:59 +0000 https://tecadmin.net/?p=27756 It’s normal that we make files and directories (or we can say folders) in our machines to keep them organized, so when we need to, we can easily search for them. Sometimes we save them with the names having spaces, for example, we save a file with the name “my file” now in this case [...]

The post Handling filenames with spaces in Linux appeared first on TecAdmin.

]]>
It’s normal that we make files and directories (or we can say folders) in our machines to keep them organized, so when we need to, we can easily search for them. Sometimes we save them with the names having spaces, for example, we save a file with the name “my file” now in this case the Linux terminal will create an error. Can files not be saved with spaces in Linux? Yes! we can but they will be accessed differently in the terminal.

This write-up is focussing on what errors we face while accessing files and directories with space in their names and how to avoid such errors.

How to create a file with spaces in its name in Linux

To understand how to reference a filename with spaces in Linux, we will consider an example. First, we will open the terminal.

Then create a file with the name “my file” by using the touch command:

touch my file 

Now see the file is being created or not by using the “ls” command. We observed that instead of one, two files have been created, one with “my” and the second with the “file” name.

To use spaces in the name we use either quotes (‘ ’) or escape sequence (\). Now we again make another file using (‘ ’) and another using (\).

touch 'my file'  
touch test\ file 

Now again use the “ls” command to view files.

Files have been created. Let’s check if we get the same errors in the creation of a directory using space or not. We will create a directory using space with the mkdir command.

mkdir new collection 

We will view whether the directory has been created or not using the “ls ” command. It created two directories instead of one.

We can rectify this in the same way as we did in the file creation method by using (‘ ‘) or (\). Again make a directory using this (‘ ‘) or (\).

mkdir my\ new\ collection 

Now we will check out the results.

How to reference filename with spaces in Linux

So we can see that the directory has been created according to our requirements. Now if we want to view the contents of the file, we simply use cat and file name with spaces; it will give us an error that the directory is not available.

cat test file 

We should use “\”. For example, we want to view the contents of a test file by using the cat command.

cat test\ file 

The file is empty so it displays no results but the command runs successfully. We can also open files by using apostrophes (‘ ’) or quotations (“ ”) as:

cat "test file" 
[OR]
cat 'test file' 

Delete filenames with Spaces in Linux

Similarly, you can also delete a file and directory with space in their name by using apostrophes ( ‘ ’ ), quotation marks (“ ”) or escape sequence ( \ ).

rm -f 'test file' 

Similarly, you can delete a directory with spaces in the name.

rm -f 'my new collection' 

Conclusion

We create files and directories, without bothering to focus on the names that can have spaces. The Linux terminal treated files and folders differently that have spaces in their names. So this article solved the problem. If we want to name a file or directory with spaces we can do it by using apostrophes ( ‘ ’ ) , quotation marks (“ ”) or escape sequence ( \ ).

The post Handling filenames with spaces in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/reference-filenams-with-spaces-in-linux/feed/ 0
how to change permissions of folder and subfolders in Linux https://tecadmin.net/change-permissions-on-folder-and-sub-folders-recursively/ https://tecadmin.net/change-permissions-on-folder-and-sub-folders-recursively/#comments Sat, 04 May 2019 10:28:30 +0000 https://tecadmin.net/?p=18415 Setting the proper file permission for any web application is an important part of web hosting. In this tutorial, you will learn how to change file permissions on folder and sub-folders recursively in a single command. As you know, In Linux everything is treated as a file. A folder is also known as directory file [...]

The post how to change permissions of folder and subfolders in Linux appeared first on TecAdmin.

]]>
Setting the proper file permission for any web application is an important part of web hosting. In this tutorial, you will learn how to change file permissions on folder and sub-folders recursively in a single command.

As you know, In Linux everything is treated as a file. A folder is also known as directory file denoted by ‘d‘ in the permission section. The below command will set the owner to www-data and group-owner to ubuntu for all files and directories and subdirectories.

sudo chown -R www-data:ubuntu /var/www/html

Use the chmod command to change the permissions for all files, directories, and subdirectories.

sudo chmod -R 755 /var/www/html

Note – The permission 755 is good to set for directories but not on files. This set the execute bit on files which is not recommended for any production environments excluded some specific cases. We recommend setting permissions separately for files and directories.

Set permissions on files:

sudo find /var/www/html -type f -exec chmod 644 {} \;

Set permissions on directories:

sudo find /var/www/html -type d -exec chmod 755 {} \;

All done.

The post how to change permissions of folder and subfolders in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/change-permissions-on-folder-and-sub-folders-recursively/feed/ 1
How to Find all Files Containing a String in Linux https://tecadmin.net/find-all-files-containing-a-string-in-linux/ https://tecadmin.net/find-all-files-containing-a-string-in-linux/#respond Tue, 04 Nov 2014 06:25:29 +0000 https://tecadmin.net/?p=6351 How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses “find” command to search string in files. Alternatively, You can also use grep command to search text. Command:- # find / -exec grep -l “string-to-search” {} ; Uses: [...]

The post How to Find all Files Containing a String in Linux appeared first on TecAdmin.

]]>
How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses “find” command to search string in files. Alternatively, You can also use grep command to search text.

Command:-


# find / -exec grep -l “string-to-search” {} ;


Uses:

Below is an example command which searches text ‘redhat’ in / filesystem. This command will search for all files containing string ‘redhat’ and list file names only like below.

# find / -exec grep -l "redhat" {} ; 

/lib64/security/pam_oddjob_mkhomedir.so
/lib64/libdevmapper.a.1.02
/lib64/libdevmapper-event.a.1.02
/lib64/libdevmapper.a
/lib64/libdevmapper-event.a
...

To Search in specific file extension files. Like search ‘redhat’ in all php files only, use following command.

# find / -name '*.php' -exec grep -l "redhat" {} ; 

/var/www/projects/index.php
/var/www/projects/new.php
...

The post How to Find all Files Containing a String in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/find-all-files-containing-a-string-in-linux/feed/ 0
How to Delete Files Older than 30 days in Linux https://tecadmin.net/delete-files-older-x-days/ https://tecadmin.net/delete-files-older-x-days/#comments Mon, 05 Aug 2013 06:35:15 +0000 https://tecadmin.net/?p=2450 Regularly cleaning out old unused files from your server is the best practice. For example, if we are running a daily/hourly backup of files or databases on the server then there will be much junk created on the server. So clean it regularly. To do it you can find older files from the backup directory [...]

The post How to Delete Files Older than 30 days in Linux appeared first on TecAdmin.

]]>
Regularly cleaning out old unused files from your server is the best practice. For example, if we are running a daily/hourly backup of files or databases on the server then there will be much junk created on the server. So clean it regularly. To do it you can find older files from the backup directory and clean them.

This article describes you to how to find and delete files older than 30 days. Here 30 days older means the last modification date is before 30 days.

1. Delete Files older Than 30 Days

Using the find command, you can search for and delete all files that have been modified more than X days. Also, if required you can delete them with a single command.

First of all, list all files older than 30 days under /opt/backup directory.

find /opt/backup -type f -mtime +30 

Verify the file list and make sure no useful file is listed in the above command. Once confirmed, you are good to go to delete those files with the following command.

find /opt/backup -type f -mtime +30 -delete 

2. Delete Files with Specific Extension

You can also specify more filters to locate commands rather than deleting all files. For example, you can only delete files with the “.log” extension and modified before 30 days.

For the safe side, first, do a dry run and list files matching the criteria.

find /var/log -name "*.log" -type f -mtime +30 

Once the list is verified, delete those files by running the following command:

find /var/log -name "*.log" -type f -mtime +30 -delete 

The above command will delete only files with a .log extension and with the last modification date older than 30 days.

3. Delete Old Directory Recursively

The -delete option may fail if the directory is not empty. In that case, we will use the Linux rm command with find to accomplish the deletion.

Searching all the directories under /var/log modified before 90 days using the command below.

find /var/log -type d -mtime +90 

Here we can execute the rm command using -exec command line option. Find command output will be sent to rm command as input.

find /var/log -type d -mtime +30 -exec rm -rf {} \; 
WARNING: Before removing the directory, Make sure no user directory is being deleted. Sometimes parent directory modification dates can be older than child directories. In that case, recursive delete can remove the child directory as well.

Conclusion

You have learned how to find and delete files in the Linux command line that have been modified more than a specified number of days ago. That will help you clean up your system from unwanted files.

The post How to Delete Files Older than 30 days in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/delete-files-older-x-days/feed/ 15
How to Remove Empty Lines from File https://tecadmin.net/how-to-remove-empty-lines-from-file/ https://tecadmin.net/how-to-remove-empty-lines-from-file/#comments Mon, 08 Jul 2013 12:49:17 +0000 https://tecadmin.net/?p=1768 Some time we need to remove empty lines from a file. Its can be done manually if file have few lines but if file have thousands of line this is hard to be done manually. Use one of following method to remove empty lines from a file. Method 1 – Using sed Sed is an [...]

The post How to Remove Empty Lines from File appeared first on TecAdmin.

]]>
Some time we need to remove empty lines from a file. Its can be done manually if file have few lines but if file have thousands of line this is hard to be done manually. Use one of following method to remove empty lines from a file.

Method 1 – Using sed

Sed is an stream editor. We can easily remove all blank lines using sed command. Use one of following sed command to remove blank lines from file. For example main.txt is your original file from which you need to remove blank lines.

Below command will remove all blank line and save content in seconf file out.txt. It will not affect the original file.

# sed '/^$/d' main.txt > out.txt

Now if you want to make changes in original file using -i switch sed command.

# sed -i '/^$/d' main.txt
    -i ( edit files in place ) Used for make changes in same file.

Method 2 – Using perl

Instead of sed, you can also use perl (a programming languege) to remove blank lines. Use the below example command to remove blank lines from main.txt file.

# perl -i -n -e "print if /S/" main.txt

Method 3 – Using awk

Also you can use AWK command line tool to remove blank lines from a file. For example use below command.

# awk 'NF > 0' main.txt > out.txt

The post How to Remove Empty Lines from File appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-remove-empty-lines-from-file/feed/ 1