How can I read a single line from a text file using bash?

  • Thread starter Thread starter Euclid
  • Start date Start date
  • Tags Tags
    Automated files
Click For Summary
SUMMARY

This discussion focuses on reading a single line from a text file using a Bash script to facilitate document backup to a remote computer. The user aims to implement a script that checks for newer file versions before backing them up. Key tools mentioned include SSH for remote connection and SCP for file transfer. The user is advised to consider using rsync for efficient file synchronization, as it optimally transfers only changed file parts.

PREREQUISITES
  • Basic knowledge of Bash scripting
  • Understanding of SSH for remote connections
  • Familiarity with SCP for secure file transfer
  • Awareness of rsync for file synchronization
NEXT STEPS
  • Learn how to read files line by line in Bash using a while loop
  • Explore the usage of SSH and SCP commands in detail
  • Investigate the features and benefits of using rsync for backups
  • Understand file modification time checks with the -nt flag in Bash
USEFUL FOR

System administrators, DevOps engineers, and anyone involved in automating file backups and synchronization processes using Bash scripting.

Euclid
Messages
213
Reaction score
0
I want to write a script that will automatically backup my documents to a remote computer. I have a lot (~500 MB) of files, so I only want the files to be backed up when a newer version exists.

Short version of the question: how do I read a single line from a text file using bash?

Long version of the question:
Here is the plan for the script...

#!/bin/bash
ssh remoteComputer # connect to remote comp
find ~/Documents * > files.txt #find all files in documents and write to files.txt
line=1
fileLength = (number of lines if files.txt)
while [line -lte fileLength]
do
(put filename at the lineth line to the string fileName)
if fileName -nt remoteComputer:~/Documents/fileName
#go through all files and
#check to see if remoteComputer has the
#latest version of this file already
scp line to remoteComputer
#if so, update file
fi
line=$[$line+1]
done
exit #leave ssh session

The exact implementation is what I need help on. If I could read files.txt line by line this would be very easy, but I can't figure it out!
 
Technology news on Phys.org
You should look at rsync, it not only does this but can copy just the parts of files that have changed.
 
That's just too easy! Thanks mgb_phys!
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
16
Views
4K
Replies
2
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K