Close Navigation
Learn more about IBKR accounts
10 R Functions for Linux Commands and Vice-versa

10 R Functions for Linux Commands and Vice-versa

Posted June 20, 2023
Andrew Treadway
TheAutomatic.net

This post will go through 10 different Linux commands and their R alternatives. If you’re interested in learning more R functions for working with files like some of those below, also check out this post.

How to list all the files in a directory

LinuxRWhat does it do?
lslist.files()Lists all the files in a directory
ls -Rlist.files(recursive = TRUE)Recursively lists all the files in a directory and all sub-directories
ls | grep “something”list.files(pattern = “something”)Lists all the files in a directory containing the regex “something”

R

list.files("/path/to/directory")
 
 
list.files("/path/to/do/directory", recursive = TRUE)
 
 
# search for files containing "something" in their name
list.files("/path/to/do/directory", pattern = "something")
 
 
# search for all CSV files
list.files("/path/to/do/directory", pattern = ".csv")

Linux

ls /path/to/directory
 
 
ls -R /path/to/directory
 
 
# search for files containing "something" in their name
ls /path/to/directory | grep "something"
 
 
# search for all CSV files
ls /path/to/directory | grep ".csv"

Getting the top records in a file / object

LinuxRWhat does it do?
headhead()Prints the top n records of a file (Linux) / data frame or other object (R)

R

# let df be a data frame
head(df)
 
head(df, 10)

Linux

head -6 some_file.txt
 
head -10 some_file.txt

Getting the current directory

LinuxRWhat does it do?
pwdgetwd()Gets the current directory

R

getwd()

Linux

pwd

Changing the directory

LinuxRWhat does it do?
cdsetwd()Change the current working directory

R

setwd("/path/to/new/directory")

Linux

cd /path/to/new/directory

How to count the number of files in a directory

LinuxRWhat does it do?
ls -1 | wc -llength(list.files(…))Counts the number of files in a directory

R

length(list.files("/path/to/some/directory"))

Linux

ls -1 | wc -l

How to check file permissions

LinuxRWhat does it do?
ls -lfile.info()Returns the file permissions (Linux) / additional info (R)

R

file.info("/path/to/directory/file.txt")

file.info returns additional information about a file besides file permissions, including size, created time, last modified time, and last access time. If you just want to get permissions of the file, just run this:

file.info("/path/to/directory/file.txt")$mode

The permissions are returned in octal; to translate what this octal result means into read / write etc. abilities, see this link.

Linux

ls -l /path/to/directory/file.txt

How to create a new directory

LinuxRWhat does it do?
mkdirdir.create()Creates a new directory

R

# create folder in current directory
dir.create("new_folder")
 
# create folder in different directory
dir.create("/path/to/new_directory")

Linux

# create folder in current directory
mkdir new_folder
 
# create folder in different directory
mkdir /path/to/new_directory

How to create a new file

LinuxRWhat does it do?
touchfile.create()Creates a new file

R

# create a file in current directory
file.create("new_file.txt")
 
# create file in different directory
file.create("/path/to/directory/new_file.txt")

Linux

# create a file in current directory
touch new_file.txt
 
# create file in different directory
touch /path/to/directory/new_file.txt

How to count the number of lines, words, and characters in a file

Though it’s possible to get the number of lines, words, and characters in a file using base R, it’s simpler to do so with the hyperSpec package.

Just use install.packages to install if needed:

install.packages("hyperSpec")

Running the below line of code will print out a data frame with the number of characters, words, and lines in the input file. Similarly, the Linux wc command will print out the same information for a file.

LinuxRWhat does it do?
wcwc()Lists the number of characters, words, and lines in a file

R

library(hyperSpec)
 
wc("/path/to/directory/file.txt")

Linux

wc /path/to/directory/file.txt

How to copy a file

LinuxRWhat does it do?
cpfile.copy()Copy a file

R

# copy file.txt to new_directory
file.copy("/path/to/directory/file.txt", "/path/to/new_directory")

Linux

# option 1
cp /path/to/directory/file.txt /path/to/new_directory
 
# option 2
cp /path/to/directory/file.txt /path/to/new_directory/file.txt

Originally posted on TheAutomatic.net.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.