file – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Sat, 31 Dec 2022 18:11:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Understanding the “/etc/shadow” File in Linux https://tecadmin.net/etc-shadow-file-in-linux/ https://tecadmin.net/etc-shadow-file-in-linux/#respond Thu, 29 Dec 2022 10:22:47 +0000 https://tecadmin.net/?p=33092 The `/etc/shadow` file in a Linux system stores password information for user accounts. It is a secure file that is readable only by the root user and is used to store the encrypted password for each user account, as well as other optional password-related information. The `/etc/shadow` file contains one line for each user account, [...]

The post Understanding the “/etc/shadow” File in Linux appeared first on TecAdmin.

]]>
The `/etc/shadow` file in a Linux system stores password information for user accounts. It is a secure file that is readable only by the root user and is used to store the encrypted password for each user account, as well as other optional password-related information.

The `/etc/shadow` file contains one line for each user account, with the fields being separated by a colon (:). The below screenshot shows the number of fields in an entry:

Understanding the /etc/shadow File
Understanding the /etc/shadow File

A basic overview of all the fields in the /etc/shadow file is as follows:

  • Username: The name of the user account.
  • Encrypted password: The encrypted password for the user account. The encrypted password is stored using a one-way hashing function, so it is not possible to retrieve the original password from the encrypted version. The initial letters of the encrypted password tell about the encryption methods used to create the password.
    • $1$: MD5
    • $2a$: Blowfish
    • $2b$: Blowfish
    • $2y$: Blowfish
    • $5$: SHA-256
    • $6$: SHA-512
    • $y$: Yescrypt
  • Last password change (date): The date on which the user last changed their password, represented as the number of days since January 1, 1970.
  • Minimum password age: The minimum number of days that must pass before the user is allowed to change their password again.
  • Maximum password age: The maximum number of days that the user’s password is valid before it must be changed.
  • Password warning period: The number of days before the user’s password is set to expire that the user will receive a warning.
  • Password inactivity period: The number of days of inactivity after which the user’s password will expire and the account will be locked.
  • Account expiration date: The date on which the user’s account will be disabled, represented as the number of days since January 1, 1970.

Note that the /etc/shadow file is only readable by the root user, so it is not possible for normal users to view the contents of this file or to retrieve the encrypted passwords of other users.

How to Update /etc/shadow File

Before we begin, it is important to note that modifying the /etc/shadow file should be done with caution, as any mistakes can potentially compromise the security of user accounts on the system. It is recommended to make a backup of the /etc/shadow file before making any changes.

  1. Change User Password
  2. We can use the `passwd` command that allows us to update the password and update /etc/shadow file. For example, to change the password of your own account simply type:

    passwd 
    

    To replace the password of the other user account, we can use the following command:

    sudo passwd USERNAME 
    

  3. Setup Password Aging
  4. Setting up password aging with the passwd command is a simple process. All you need to do is open a terminal window and type in the following command:

    ## Syntax
    passwd -l USERNAME -u NUMBER_OF_DAYS -x NUMBER_OF_DAYS

    Replace USERNAME with the name of the user whose password you want to set up aging for. Replace NUMBER_OF_DAYS with the number of days you want the user’s password to remain valid. For example, if you want the user’s password to expire after 90 days, you would use the command:

    passwd -l USERNAME -u 90 -x 7 
    

    This command will set the maximum password lifetime to 90 days and the password expiration warning period to 7 days.

    Once you have set up the password aging rules, you can check the status of the user’s password with the following command:

    passwd -S USERNAME
    

    This command will give you information about the user’s password, including when it will expire and the maximum password lifetime.

Conclusion

In this article, you have learned about the `/etc/shadow` file in a Linux system. You know about the detail of the fields in a single entry of the shadow file. Along with that, you got to know about password aging in Linux.

The post Understanding the “/etc/shadow” File in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/etc-shadow-file-in-linux/feed/ 0
How to Append Data to File in Python https://tecadmin.net/python-append-file/ https://tecadmin.net/python-append-file/#respond Tue, 27 Dec 2022 02:51:20 +0000 https://tecadmin.net/?p=33131 In Python, you can use the `open()` function to open a file in append mode, which allows you to add new content to the end of an existing file. Appending to a file is useful when you want to add additional information to a file without modifying or deleting the file’s original content. How to [...]

The post How to Append Data to File in Python appeared first on TecAdmin.

]]>
In Python, you can use the `open()` function to open a file in append mode, which allows you to add new content to the end of an existing file. Appending to a file is useful when you want to add additional information to a file without modifying or deleting the file’s original content.

How to Appened Data to File in Python

In this article we will discuss the following methods to append content to files in Python:

  • write() method
  • writelines() method
  • bytes() method

  1. Using `write()` method
  2. To open a file in append mode, you can use the ‘a’ mode parameter when calling the `open()` function. For example:

    # Open the file in append mode
    with open('myfile.txt', 'a') as f:
      # Write the new content to the file
      f.write('This is new content being added to the file.\n')

    In this example, the file myfile.txt is opened in append mode using the ‘a’ mode parameter. The file is then opened using a `with` statement, which ensures that the file is properly closed after the operations inside the with block are completed.

    Inside the with block, you can use the file object’s `write()` method to add new content to the end of the file. The write() method takes a string as an argument and writes it to the file. In this example, the string ‘This is new content being added to the file.\n’ is written at the end of the file.

    Note that if the file does not exist, it will be created when you open it in append mode.

  3. Using `writelines()` method
  4. You can also use the `writelines()` method to writing multiple lines of text to a file in append mode. The writelines() method takes a list of strings as an argument, and writes each string in the list to the file, with a newline character added after each string.

    Here is an example of using the `writelines()` method to append multiple lines of text to a file:

    # Open the file in append mode
    with open('myfile.txt', 'a') as f:
      # Write multiple lines of text to the file
      f.writelines(['This is the first line.\n', 'This is the second line.\n'])

    In this example, the strings ‘This is the first line.\n’ and ‘This is the second line.\n’ are added to the end of the file myfile.txt.

    It’s important to note that when you open a file in append mode, any existing content in the file is not modified or deleted. Only the new content that you write to the file is added to the end of the file.

  5. Using `append()` method
  6. You can also use the `append()` method of a file object to append new content to a file. The append() method takes a string as an argument and writes it to the end of the file, just like the write() method.

    Here is an example of using the `append()` method to add new content to a file:

    # Open the file in append mode
    with open('myfile.txt', 'a') as f:
      # Append new content to the file
      f.append('This is new content being added to the file.\n')

    In this example, the string ‘This is new content being added to the file.\n’ is added to the end of the file myfile.txt.

    Overall, appending to a file in Python is a simple and straightforward process.

Editing File in Read/Write Mode in Python

There are a few things to keep in mind when appending to a file in Python.

  • First, it’s important to make sure that you have the correct permissions to modify the file. If you don’t have the necessary permissions, you may receive an error when trying to append to the file.
  • Second, it’s a good idea to use the with statement when working with files in Python, as it ensures that the file is properly closed after the operations inside the with block are completed. This is especially important when working with large files, as it can help prevent memory leaks and other issues.
  • Finally, it’s worth noting that you can also use the a+ mode parameter when opening a file in Python to open the file in append mode and read mode. This allows you to both add new content to the end of the file and read the file’s existing content.

Here is an example of using the a+ mode parameter to open a file in append and read mode:

# Open the file in append and read mode
with open('myfile.txt', 'a+') as f:
  # Read the existing content of the file
  existing_content = f.read()
  # Print the existing content
  print(existing_content)
  
  # Append new content to the file
  f.write('This is new content being added to the file.\n')

In this example, the file myfile.txt is opened in both append and read mode using the ‘a+’ mode parameter. The file is then opened using a `with` statement, and the `read()` method is used to read the file’s existing content. The existing content is then printed to the console using the `print()` function.

Finally, the `write()` method is used to append new content to the end of the file. The new content is added to the end of the file, and the file’s original content is left unchanged.

Conclusion

Overall, appending to a file in Python is a simple and powerful way to add new content to an existing file without overwriting the file’s original content. Whether you are using the `write()` or `append()` method, or opening the file in append mode using the ‘a’ or ‘a+’ mode parameter, you can easily add new content to a file in Python.

The post How to Append Data to File in Python appeared first on TecAdmin.

]]>
https://tecadmin.net/python-append-file/feed/ 0
Efficiently Reading a File Line by Line in a Shell Script https://tecadmin.net/reading-file-line-by-line-in-linux-shell-script/ https://tecadmin.net/reading-file-line-by-line-in-linux-shell-script/#respond Fri, 16 Dec 2022 10:52:21 +0000 https://tecadmin.net/?p=6390 Reading a file line by line is a common task in many shell scripts, as it allows you to process each line of a file separately and perform actions based on the contents of each line. There are several ways to read a file line by line in a Linux shell script, but some methods [...]

The post Efficiently Reading a File Line by Line in a Shell Script appeared first on TecAdmin.

]]>
Reading a file line by line is a common task in many shell scripts, as it allows you to process each line of a file separately and perform actions based on the contents of each line. There are several ways to read a file line by line in a Linux shell script, but some methods are more efficient than others. In this article, we’ll explore some of the most efficient ways to read a file line by line in a Linux shell script.

Using while loop

The most basic way to read a file line by line in a shell script is to use a while loop and the read command. The read command reads a line of input from the file and stores it in a variable, which can then be processed by the script. The while loop allows you to iterate through the lines of the file until the end is reached. Here’s an example of how this might look:

#!/usr/bin/env bash

# Read file line by line
while read line; do
  # Process single line content 
  echo "$line"
done < file.txt

This method is simple and easy to understand, but it has some limitations. One limitation is that the read command can only read one line at a time, so it can be slower for large files with many lines. In addition, the read command can only read from standard input (stdin), so you must use the < operator to redirect the contents of the file to stdin. This can be inconvenient if you need to read from multiple files or if you want to read from a file that is not in the current directory.

Using while loop with cat

A more efficient way to read a file line by line in a shell script is to use the cat command in combination with a while loop. The cat command reads a file and outputs its contents to stdout, which can then be processed by the while loop. Here’s an example of how this might look:

#!/usr/bin/env bash

# Read file line by line
while read line; do
  # Process single line content 
  echo "$line"
done < <(cat file.txt)

This method is more efficient than the read command because it reads the entire file into memory at once, rather than reading one line at a time. This can be faster for large files with many lines. In addition, the cat command can read from any file, not just stdin, so you can use it to read from multiple files or files that are not in the current directory.

Using while loop with sed

Another efficient way to read a file line by line in a shell script is to use the sed command in combination with a while loop. The sed command is a powerful text processing tool that can read a file and output its contents to stdout, one line at a time. Here’s an example of how this might look:

#!/usr/bin/env bash

# Read file line by line
while read line; do
  # Process single line content 
  echo "$line"
done < <(sed -n -e 1p file.txt)

This method is similar to the cat command, but it is more efficient because it only outputs one line at a time. This can be faster for large files with many lines. In addition, the sed command has many options and features that can be used to manipulate the output, so it is a very flexible tool for text processing.

In summary, there are several ways to read a file line by line in a Linux shell script.

The post Efficiently Reading a File Line by Line in a Shell Script appeared first on TecAdmin.

]]>
https://tecadmin.net/reading-file-line-by-line-in-linux-shell-script/feed/ 0
How To Create SFTP User for a Web Server Document Root https://tecadmin.net/how-to-create-sftp-user-for-a-web-server-document-root/ https://tecadmin.net/how-to-create-sftp-user-for-a-web-server-document-root/#comments Mon, 12 Jul 2021 10:43:41 +0000 https://tecadmin.net/?p=25790 SFTP (SSH/Secure File Transfer Protocol) is a network transmission standard used to transfer, access, and manage files over a remote network. It contains SSH (Secure Shell), making it a lot more secure than the FTP protocol. Files are transferred through a single control channel in SFTP. It requires authentication and runs on port 22. In [...]

The post How To Create SFTP User for a Web Server Document Root appeared first on TecAdmin.

]]>
SFTP (SSH/Secure File Transfer Protocol) is a network transmission standard used to transfer, access, and manage files over a remote network. It contains SSH (Secure Shell), making it a lot more secure than the FTP protocol.

Files are transferred through a single control channel in SFTP. It requires authentication and runs on port 22. In SFTP, the SSH shell provides encryption that helps protect usernames, passwords, and other personal data transferred through SFTP.

In this how-to guide, we will learn to create SFTP users for web server document root.

Step 1 – Installing SSH (Secure Shell)

SFTP is a very secure file transfer protocol because of the encryption that SSH provides for the data as it is transferred over the network. SSH is mainly installed on Linux distributions by default, but if it is not pre-installed in your system, then you can use the below-given command to install it:

sudo apt install ssh 

Installing SSH Server

If already installed, the command will upgrade OpenSSH packages.

Step 2 – Configuring SSH to use the SFTP Server Code

Now open the configuration file of SSH in a text editor to modify it for SFTP server code. Here we will use the nano editor to edit the configuration file.

sudo nano /etc/ssh/sshd_config 

Locate the line starting from “Subsystem sftp”.

Locate subsystem in ssh config

Comment the line by adding # at the start of the line and write the following line after this line as shown in the screenshot given below:

Subsystem sftp internal-sftp

Adding Subsystem sftp internal-sftp

The SSHD will use the SFTP server code instead of running the SFTP server by changing the above line.

Once you have changed the configuration file, save the file and exit from it using the keyboard shortcut keys CTRL+S and CTRL+X.

After changes, we need to restart the SSHD daemon to let the changes work.

sudo systemctl restart sshd 

Step 3 – Creating a User(SFTP User)

It is an excellent practice to create a new user that only has SFTP access to the document root. It is not recommended to add a user with Sudo privileges to the webserver document root. Create a new user using the adduser command:

sudo adduser sftpuser 

The terminal will ask for a couple of things like setting the password, and user information. It will also ask for a few other details, so either leave them empty or provide the proper information.

Sudo adduser sftp

A new user with the name of sftpuser is successfully created.

Step 4 – Creating Match User Directive in the SSH configuration file

Now we will restrict this user to the document root and we will also disable the user’s access to SSH so that the user will log in through SFTP.

To restrict the user’s access, open up the configuration file of SSH in any text editor:

sudo nano /etc/ssh/sshd_config 

Now go to the end of the file and add the following content in the “sshd_config” configuration file:

Match User sftpuser
        ForceCommand internal-sftp 
        ChrootDirectory /var/www/
        PasswordAuthentication yes
        X11Forwarding no 
        AllowTcpForwarding no 

Make sure to replace the “sftpuser” username with the username you set.

Match User sftpuser in sshd_config

Once the above content is added to the SSH configuration file, save and exit using CTRL+S and CTRL+X shortcut keys.

To check the syntax and verify if everything went well, you can execute the command:

sudo sshd -t 

If no error occurred, we could reload the SSH service for the changes to work.

sudo systemctl restart sshd 

Step 5 – Adding SFTP User to www-data Group

Now we will add the user to the www-data group by executing the following command:

sudo usermod -a -G www-data sftpuser 

On a successful run, no output will be displayed.

Step 6 – Setting Document Root Directory Permission

Please follow the subsequent instructions very carefully as SFTP is very strict regarding chroot directory permissions.

  1. We will start by checking the current permissions and ownership of var:
    sudo ls -ld /var/ 
    
  2. The permissions should be 755 and the owner should be root by default. If not, then execute the command given below to set the proper permissions:
    sudo chmod 755 /var/ 
    
  3. Now use this command to set the correct ownership:
    sudo chown root:root /var/ 
    
  4. Similarly, apply the same permissions to the chroot:

    sudo chmod 755 /var/www/ 
    
  5. Since we have set “/var/www/” to the chroot directory. Now set the right ownership of the chroot directory:
    sudo chown root:root /var/www/ 
    
  6. To allow a group to write to the document root directory, set its permission to 755:
    sudo chmod 755 /var/www/html/ 
    
  7. To grant the ownership of the “/var/www/html” document root and its further directories and files to the www-data group, use the below-given command:
    sudo chown -R www-data:www-data /var/www/html* 
    
  8. Now give 755 permissions to the content placed in the “/var/www/html” document root using the command:
    sudo find /var/www/html/ -type d -exec chmod 775 {} \; 
    
  9. The above command will grant the SFTP user read, write, and executable permissions of the directories.

    We also need to give 664 permissions to all the files that are present in the document root to allow the owner and the SFTP users’ group to read and write the files:

    sudo find /var/www/html/ -type f -exec chmod 664 {} \; 
    
  10. Now for the last step, make certain that all the new files and directories acquire the www-data group that are created the newly created SFTP user:
    sudo find /var/www/html -type d -exec chmod g+s {} \; 
    

Congratulations! your new SFTP user has been created and added to the webserver document root. You can now log in to SFTP.

Conclusion

In this how-to guide, we have learned how to install and configure SSH for using the SFTP server code. After that, we created a new user, restricted them to document root, and disabled their SSH access. Then we added the user to the webserver document root to allow the user to read, write and execute files in the document root.

The post How To Create SFTP User for a Web Server Document Root appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-create-sftp-user-for-a-web-server-document-root/feed/ 1
Chattr Command in Linux with Examples https://tecadmin.net/linux-chattr-command/ https://tecadmin.net/linux-chattr-command/#comments Wed, 07 Jul 2021 10:41:14 +0000 https://tecadmin.net/?p=511 The “chattr”, short for change attribute, is a command-line utility in Linux used to change attributes of a file e.g a, i. This command is primarily used to make various files immutable and undeletable for regular users. File management is a complicated process in Linux as it is a multi-user operating system. The administrators can [...]

The post Chattr Command in Linux with Examples appeared first on TecAdmin.

]]>
The “chattr”, short for change attribute, is a command-line utility in Linux used to change attributes of a file e.g a, i. This command is primarily used to make various files immutable and undeletable for regular users.

File management is a complicated process in Linux as it is a multi-user operating system. The administrators can change the attributes of a file using the “chattr” command so it cannot be accessed and changed by anyone except the superuser. This saves the important files from accidental deletion.

In this write-up, we will focus on how to modify the attributes of a file by using the “chattr” command. We will also learn about different flags that can be used along with the “chattr” command. But first, let’s discuss the syntax of the “chattr” command:

chattr [OPERATOR][Flags] FILE

Flags

Here is a list of the most common flags and attributes:

  • 'a' With this attribute a file can only be opened in append mode.
  • 'i' To make a file immutable
  • 'S' Files with this attribute are synchronously updated on the disk
  • 'u' To save contents of a file when it is deleted
  • 't' To restrict tail merging
  • 'j' The data of files with this attribute is updated to ext3 journal before the file itself

Operators

  • '+' This operator is used to add additional attributes.
  • '-' This operator is used to remove attributes of a file.
  • '=' This operator is used to make the specified attributes, the only attributes of the file.

How to use ‘i’ attribute to make a file immutable

The “chattr” command is often used to make files immutable. Immutable means that the file cannot be moved, renamed, or deleted.

Here we will give the ‘i’ flag to a file named “test-file.txt” as an example:

sudo chattr +i test-file.txt 

Chattr Command in Linux 001

You can use the “lsattr” to check the file’s attributes.

As you can see in the screenshot above the ‘i’ attribute has been set and the file has become immutable.

The ‘i’ attribute can also be used to make directories immutable.

How to remove the ‘i’ attribute from the file

Once the ‘i’ attribute has been set the file can only be changed or deleted once the attribute is removed by the root user. Use the ‘-’ operator with the option to remove the attribute:

sudo chattr -i test-file.txt 

Chattr Command in Linux 002

How to use the ‘a’ attribute to open file in append mode

We can use the ‘a’ attribute to open the file in the append mode. In append mode, users can only append Data on a file without changing the data that is already present in the file.

sudo chattr +a test-file.txt 

Chattr Command in Linux 003

Now, as you can see in the screenshot below when I try to add more data into the text file by using the echo command the terminal gives me an error:

Chattr Command in Linux 005

But we can append data into the file by using “>>” instead of “>” operator:

Chattr Command in Linux 005

How to add ‘j’ attribute to update data of the file to ext3 journal

By using the ‘j’ attribute, the data of the files attribute will be updated to the ext3 journal before the file itself:

sudo chattr +j test-file.txt 

Chattr Command in Linux 006

Conclusion

The “chattr” command is a very useful tool for administrators. It enables them to modify file permissions which helps in the protection of important files and prevents them from being altered.

In this write-up, we discussed what the ‘chattr’ command is and how to use it. Moreover, we also discussed some important flags that are used along with the ‘chattr’ command.

The post Chattr Command in Linux with Examples appeared first on TecAdmin.

]]>
https://tecadmin.net/linux-chattr-command/feed/ 1
How to find files larger than 10MB, 100MB, 1GB in Linux https://tecadmin.net/find-all-files-larger-than-1gb-size-in-linux/ https://tecadmin.net/find-all-files-larger-than-1gb-size-in-linux/#comments Fri, 21 Aug 2020 13:12:18 +0000 https://tecadmin.net/?p=22373 If you’re looking for files that are larger than 10MB, 100MB or 1GB, the find command can be very helpful. With find, you can search for files based on size criteria. A few days back my production application goes down. After searching for half an hour, I found the application was down due to the [...]

The post How to find files larger than 10MB, 100MB, 1GB in Linux appeared first on TecAdmin.

]]>
If you’re looking for files that are larger than 10MB, 100MB or 1GB, the find command can be very helpful. With find, you can search for files based on size criteria.

A few days back my production application goes down. After searching for half an hour, I found the application was down due to the disk full on my server. So I searched all files greater than 1 GB and then all files greater than 100 MB. There were a few log files that were large in size, which caused the disk full.

In this tutorial, you will learn how to search file by their size using find command.

Searching the larger files in Linux

You can define size in KB, MB and GB formats. For example, you can define size 100K, 100M, 1G or 10G formats. Use below examples, which will help you to find files by there size and extension.

  • The following command will find all file greater than equals to 100MB under entire file system.
    find / -size +100M 
    

    This would search through the entire file system and return a list of all files that are larger than 100MB. If you only want to search a specific directory, you can replace “/” with the path to that directory. For example, if you only wanted to search your home directory, you could use this command:

    find ~/ -size +100M 
    
  • You can also use find to search for files that are larger than 1GB. To do this, you would just need to use a different size criterion. For example, to find all files that are greater than 1GB, you could use this command:
    find / -size +1G 
    

Find files by size and extension

Instead of searching all files, you can also search files of specific extensions greater than 1G B size. For example search, all files with extension “.log” and size are 1GB or more.

find / -type f -name "*.log" -size +1G 

Related Topics

The post How to find files larger than 10MB, 100MB, 1GB in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/find-all-files-larger-than-1gb-size-in-linux/feed/ 1
Find All files with 777 permission in Linux https://tecadmin.net/find-files-with-permission-777-in-linux/ https://tecadmin.net/find-files-with-permission-777-in-linux/#respond Thu, 13 Aug 2020 16:24:05 +0000 https://tecadmin.net/?p=22302 Right file permission is the most crucial part of the Linux system management. A file with permission 777 is open to everyone for read and write. Any user logged in to system can write to this file. Which can be harmful for your system. In some condition’s, you may required 777 permissions like log file [...]

The post Find All files with 777 permission in Linux appeared first on TecAdmin.

]]>
Right file permission is the most crucial part of the Linux system management. A file with permission 777 is open to everyone for read and write. Any user logged in to system can write to this file. Which can be harmful for your system.

In some condition’s, you may required 777 permissions like log file etc. But mostly we don’t required it. This tutorial will help you to search files with 777 permission on your Linux/Unix system via find command.

Syntax:

find /path/to/dir -perm 777 

The -perm command line parameter is used with find command to search files based on permissions. You can use any permission instead of 777 to find files with that permissions only.

For example to search all files with permission 777 under the logged in user home directory, type:

find $HOME -perm 777 

The above command will search all files and directories with permission 777 under the specified directory.

Linux command find files with 777 permissions

But if you don’t want to include directories in this list. Define the type with -type on command line parameter as below. This will search only files with permission 777 under the /var/www directory.

find /var/www -perm 777 -type f 

Linux command find files only with 777 permissions

To search directory only, type:

find /var/www -perm 777 -type d 

Hope this tutorial helps you search files based on permissions and secure your Linux/Unix system.

The post Find All files with 777 permission in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/find-files-with-permission-777-in-linux/feed/ 0
How to Write/Append Multiple Lines to a File on Linux https://tecadmin.net/write-append-multiple-lines-to-file-linux/ https://tecadmin.net/write-append-multiple-lines-to-file-linux/#comments Thu, 14 Sep 2017 17:58:50 +0000 https://tecadmin.net/?p=13508 Sometimes you may be required to write or append multiple lines to a file. You can use multiple methods to write multiple lines to a file through the command line in the Linux system. Here are the three methods described below. Method 1:- You can write/append content line by line using the multiple echo commands. [...]

The post How to Write/Append Multiple Lines to a File on Linux appeared first on TecAdmin.

]]>
Sometimes you may be required to write or append multiple lines to a file. You can use multiple methods to write multiple lines to a file through the command line in the Linux system. Here are the three methods described below.

Method 1:-

You can write/append content line by line using the multiple echo commands. This the simple and straight forward solution to do this. We recommend going with Method 2 or Method 3.

echo "line 1 content" >> myfile.txt
echo "line 2 content" >> myfile.txt
echo "line 3 content" >> myfile.txt

Method 2:-

You can append content with the multi-line command in the quoted text. In this way can write multiple lines to fine with single echo command. But the third method is our suggested method to do this.

echo "line 1 content
line 2 content
line 3 content" >> myfile.txt

Method 3:-

This is the third and suggested option to use here documents (<<) to write a block of multiple lines text in a single command. The above methods are also useful but personally, I also use this while working.

cat >> greetings.txt <<EOL
line 1 content
line 2 content
line 3 content
EOL

The post How to Write/Append Multiple Lines to a File on Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/write-append-multiple-lines-to-file-linux/feed/ 11
How to Download and Upload Files with SFTP Securely https://tecadmin.net/download-and-upload-files-with-sftp/ https://tecadmin.net/download-and-upload-files-with-sftp/#comments Wed, 08 Mar 2017 18:31:41 +0000 https://tecadmin.net/?p=11602 SFTP (SSH File Transfer Protocol) is secured protocol to transfer files between local and remote server. To required SSH server running on the remote system. This protocol encrypts the transfer of data between local and remote system. As SFTP provides secure data transfer, so we recommend it over FTP protocol. SFTP is recommended but in [...]

The post How to Download and Upload Files with SFTP Securely appeared first on TecAdmin.

]]>
SFTP (SSH File Transfer Protocol) is secured protocol to transfer files between local and remote server. To required SSH server running on the remote system. This protocol encrypts the transfer of data between local and remote system. As SFTP provides secure data transfer, so we recommend it over FTP protocol.

SFTP is recommended but in case you only have the FTP server running on remote, use below link for FTP access.

Connect to SFTP Server:

SFTP connects to ssh server. You must have the ssh server running on the remote system. Use the following command to connect example.com server as user rahul.

$ sftp rahul@example.com

to connect with different port

$ sftp -P 2222 rahul@example.com

After successful authentication, you will get a sftp prompt. Where you can download or upload files securely. To get available commands type help on sftp prompt.

sftp> help 

Available commands:
bye                         Quit sftp
cd path                     Change remote directory to 'path'
chgrp grp path              Change group of file 'path' to 'grp'
chmod mode path             Change permissions of file 'path' to 'mode'
chown own path              Change owner of file 'path' to 'own'
df [-hi] [path]             Display statistics for current directory or
...
...

Change Local and Remote Directory

First check your local and remote server directory using following commands.

sftp> !pwd
/home/ubuntu

sftp> pwd
/home/rahul
  • !pwd – Used to check current directory on local system
  • pwd – Used to check current directory on remote system

Now navigate between directories on local and remote sftp system.

sftp> lcd /home/ubuntu/Downloads

sftp> cd Uploads
  • lcd – Used to navigate between directories on local system
  • cd – Used to navigate between directories on remote system

Download Files from SFTP

Use get command to download file from sftp server to local system drive. Use lcd to change location of local download folder. Below command will download remotefile.txt from remote system to local system.

sftp> get remotefile.txt

To download files and folders recursively use -r switch with get command. Below command will download folder remotedir from remote system to local system recursively.

sftp> get -r remotedir

Upload Files to SFTP

Use put command to upload a file from local system to remote system. Use cd to change location of remote upload folder first. the below command will upload localfile.txt from local system to remote sftp system.

sftp> put localfile.txt

To upload files and folders recursively use -r switch with put command. Below command will upload directory localdir and all files and sub directories to remote server.

sftp> put -r localdir

The post How to Download and Upload Files with SFTP Securely appeared first on TecAdmin.

]]>
https://tecadmin.net/download-and-upload-files-with-sftp/feed/ 1
PHP fgets() Function: Read file line by line https://tecadmin.net/php-read-file-line-by-line-fgets-function/ https://tecadmin.net/php-read-file-line-by-line-fgets-function/#comments Sun, 04 Sep 2016 04:08:58 +0000 https://tecadmin.net/?p=10781 PHP fgets() function is used for reading a single line from a file. This function takes two arguments as described below. 1. File specifies the filename to read content 2. Length Optional parameter specifies the number of bytes to read from the file. Syntax PHP fgets() function uses the following syntax. fgets(file, length) Example – [...]

The post PHP fgets() Function: Read file line by line appeared first on TecAdmin.

]]>
PHP fgets() function is used for reading a single line from a file. This function takes two arguments as described below.

1. File specifies the filename to read content
2. Length Optional parameter specifies the number of bytes to read from the file.

Syntax

PHP fgets() function uses the following syntax.

  fgets(file, length)

Example – Read a single line

Below are two examples of a reading single line from a file. Use this example to read the complete first-line content from a file.

<?php
  $fn = fopen("/var/data/myfile.txt","r");
  $result = fgets($fn);
  echo $result;
  fclose($fn);
?>

Instead of a line, you can also define the number of bytes to read from a file:

<?php
  $fn = fopen("/var/data/myfile.txt","r");
  $result = fgets($fn, 20);
  echo $result;
  fclose($fn);
?>

Example – Read All Lines

Use this example to read all lines from files one by one using for loop. Use PHP feof() function to find the end of the file.

<?php
  $fn = fopen("/var/data/myfile.txt","r");
  
  while(! feof($fn))  {
    $result = fgets($fn);
    echo $result;
  }

  fclose($fn);
?>

The post PHP fgets() Function: Read file line by line appeared first on TecAdmin.

]]>
https://tecadmin.net/php-read-file-line-by-line-fgets-function/feed/ 3