l2t

Useful file operations in Linux/Mac

Perform different file operations in linux operating system.

In this tutorial we will learn about how we can perform different file operation on linux based operating system.

For demo purpose we will use following movies csv file:

Movies.csv

List the file contents

Let's look at how we can check the file data in different ways:

# view all file contents
cat movies.csv

# view a file with line numbers
cat -n movies.csv

# omit the repeated empty output lines
cat -s movies.csv

# read only 10 lines from the beginning of the file
head movies.csv

# read last 10 lines of the file
tail movies.csv

# read n number of lines from beginning or end of the file
head -n movies.csv   i.e. replace n with a number
tail -n movies.csv   i.e. replace n with a number

# read data from the file
# with the line number
# scroll down to view more data
# it works like pagination
less -N movies.csv
more -N movies.csv

# different approach
cat movies.csv | less
cat movies.csv | more

Filter the file contents

Now, we know how to display file contents however if we want to filter some words or string or replace etc.. We would use following operations:

# let's only display movie years
# we know that our file is comma delimitor
# and movie years are stored in second field
cat movies.csv | cut -d"," -f2

# let's go more advance and only want to find
# movies that ends with 99
cat movies.csv | grep '99\b'
cat movies.csv | egrep '99\b'

# fine movies that starts with 19
cat movies.csv | grep '\b19'
cat movies.csv | egrep '\b19'

# Find all movies that contains The word
cat movies.csv | grep -w The

# Find all movies that has The word
# Also print the line number in the beginning
cat movies.csv | grep -nw The

# Get all movies that contains The word
# Print the line numbers also and then filter
# Only movies that has King word in them
cat movies.csv | grep -nw The | grep -w King

Replace words in file

To replace words from the filtered result following commands can be used:

# Search movies that has Beauty word
# Replace Beauty with Phycho word
cat movies.csv | grep Beauty | sed 's/Beauty/Phycho/g'

# Find all 19's movies
# Filter only Dogs movies
# Replace Dogs with Cats
# Create a new file with the results
cat movies.csv | grep '\b19' | grep -w Dogs | sed 's/Dogs/Cats/g' > results.csv

I hope you enjoyed this tutorial please like and share this tutorial.